groovy.sql.GroovyRowResult Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of groovy-sql Show documentation
Show all versions of groovy-sql Show documentation
Groovy: A powerful multi-faceted language for the JVM
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package groovy.sql;
import groovy.lang.GroovyObjectSupport;
import groovy.lang.MissingPropertyException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* Represents an extent of objects.
* It's primarily used by methods of Groovy's {@link groovy.sql.Sql} class to return {@code ResultSet} data in map
* form; allowing access to the result of a SQL query by the name of the column, or by the column number.
*/
public class GroovyRowResult extends GroovyObjectSupport implements Map {
private final Map result;
public GroovyRowResult(Map result) {
this.result = result;
}
/**
* Retrieve the value of the property by its (case-insensitive) name.
*
* @param property is the name of the property to look at
* @return the value of the property
*/
@Override
public Object getProperty(String property) {
try {
Object key = lookupKeyIgnoringCase(property);
if (key != null) {
return result.get(key);
}
throw new MissingPropertyException(property, GroovyRowResult.class);
}
catch (Exception e) {
throw new MissingPropertyException(property, GroovyRowResult.class, e);
}
}
private Object lookupKeyIgnoringCase(Object key) {
// try some special cases first for efficiency
if (result.containsKey(key))
return key;
if (!(key instanceof CharSequence))
return null;
String keyStr = key.toString();
for (Object next : result.keySet()) {
if (!(next instanceof String))
continue;
if (keyStr.equalsIgnoreCase((String)next))
return next;
}
return null;
}
/**
* Retrieve the value of the property by its index.
* A negative index will count backwards from the last column.
*
* @param index is the number of the column to look at
* @return the value of the property
*/
public Object getAt(int index) {
try {
// a negative index will count backwards from the last column.
if (index < 0)
index += result.size();
Iterator