001 /* 002 $Id: GroovyRowResult.java,v 1.1 2005/02/23 22:09:15 glaforge Exp $ 003 004 Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved. 005 006 Redistribution and use of this software and associated documentation 007 ("Software"), with or without modification, are permitted provided 008 that the following conditions are met: 009 010 1. Redistributions of source code must retain copyright 011 statements and notices. Redistributions must also contain a 012 copy of this document. 013 014 2. Redistributions in binary form must reproduce the 015 above copyright notice, this list of conditions and the 016 following disclaimer in the documentation and/or other 017 materials provided with the distribution. 018 019 3. The name "groovy" must not be used to endorse or promote 020 products derived from this Software without prior written 021 permission of The Codehaus. For written permission, 022 please contact info@codehaus.org. 023 024 4. Products derived from this Software may not be called "groovy" 025 nor may "groovy" appear in their names without prior written 026 permission of The Codehaus. "groovy" is a registered 027 trademark of The Codehaus. 028 029 5. Due credit should be given to The Codehaus - 030 http://groovy.codehaus.org/ 031 032 THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS 033 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 034 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 035 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 036 THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 037 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 038 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 039 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 040 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 041 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 042 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 043 OF THE POSSIBILITY OF SUCH DAMAGE. 044 045 */ 046 package groovy.sql; 047 048 import groovy.lang.GroovyObjectSupport; 049 import groovy.lang.MissingPropertyException; 050 051 import java.util.Iterator; 052 import java.util.LinkedHashMap; 053 054 /** 055 * Represents an extent of objects. 056 * It's used in the oneRow method to be able to access the result 057 * of a SQL query by the name of the column, or by the column number. 058 * 059 * @Author Jean-Louis Berliet 060 * @version $Revision: 1.1 $ 061 */ 062 public class GroovyRowResult extends GroovyObjectSupport { 063 064 private LinkedHashMap result; 065 066 public GroovyRowResult(LinkedHashMap result) { 067 this.result = result; 068 } 069 070 /** 071 * Retrieve the value of the property by its name * 072 * @param property is the name of the property to look at 073 * @return the value of the property 074 */ 075 public Object getProperty(String property) { 076 try { 077 Object value = result.get(property); 078 if (value==null) { 079 // with some databases/drivers, the columns names are stored uppercase. 080 value = result.get(property.toUpperCase()); 081 if (value==null) 082 throw new MissingPropertyException(property, GroovyRowResult.class); 083 } 084 return(value); 085 } 086 catch (Exception e) { 087 throw new MissingPropertyException(property, GroovyRowResult.class, e); 088 } 089 } 090 091 /** 092 * Retrieve the value of the property by its index. 093 * A negative index will count backwards from the last column. 094 * @param index is the number of the column to look at 095 * @return the value of the property 096 */ 097 public Object getAt(int index) { 098 try { 099 // a negative index will count backwards from the last column. 100 if (index<0) 101 index += result.size(); 102 Iterator it = result.values().iterator(); 103 int i = 0; 104 Object obj = null; 105 while ( (obj==null) && (it.hasNext()) ) { 106 if (i==index) 107 obj = it.next(); 108 else 109 it.next(); 110 i++; 111 } 112 return(obj); 113 } 114 catch (Exception e) { 115 throw new MissingPropertyException(Integer.toString(index), GroovyRowResult.class, e); 116 } 117 } 118 119 public String toString() { 120 return(result.toString() ); 121 } 122 123 }