www.pudn.com > town-1[1].0.4.rar > KeyDef.java
package com.workingdogs.town; import java.util.Vector; /* Town, a Java JDBC abstraction layer Copyright (C) 1999 Serge Knystautas, Jon S. Stevens This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /** A KeyDef is a way to define the key columns in a table. The KeyDef is generally used in conjunction with a TableDataSet. Essentially a KeyDef is what forms the WHERE clause for an UPDATE or DELETE.In order to use the KeyDef, you simply use it like this:
KeyDef kd = new KeyDef().addAttrib("key_column_a"); TableDataSet tds = new TableDataSet ( connection, "table", kd ); Record rec = tds.getRecord(0); rec.setValue("column_name", "new value" ); rec.save(); tds.close();In the above example, Record 0 is retrieved from the database table and the following update statement is generated:UPDATE table SET column_name=? WHERE key_column_a=?
@author Jon S. Stevens jon@working-dogs.com @author Serge Knystautas sergek@lokitech.com @version 1.0 */ public class KeyDef { Vector data = new Vector (); public KeyDef() { super(); // KeyDef is 1 based. data.addElement (""); } /** Adds the named attribute to the KeyDef. @returns a copy of itself */ public KeyDef addAttrib(String name) { data.addElement (name); return this; } /** Determines if the KeyDef contains the requested Attribute. @returns true if the attribute has been defined. false otherwise. */ public boolean containsAttrib (String name) { return !(data.indexOf ((Object) name) == -1); } /** getAttrib is 1 based. Setting pos to 0 will attempt to return pos 1. @returns value of Attribute at pos as String. null if value is not found. */ public String getAttrib (int pos) { if (pos == 0) pos = 1; try { return (String) data.elementAt (pos); } catch (ArrayIndexOutOfBoundsException e) { return null; } } /** KeyDef's are 1 based, so this overrides the Vector.size() method and returns size - 1; @returns the number of elements in the KeyDef that were set by addAttrib() @see #addAttrib(java.lang.String) */ public int size() { return data.size() - 1; } }