All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.avaje.ebeaninternal.server.query.DefaultSqlRow Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) 2006  Robin Bygrave
 * 
 * This file is part of Ebean.
 * 
 * Ebean is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *  
 * Ebean 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 Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with Ebean; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA  
 */
package com.avaje.ebeaninternal.server.query;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import com.avaje.ebean.SqlQuery;
import com.avaje.ebean.SqlRow;
import com.avaje.ebeaninternal.server.core.BasicTypeConverter;

/**
 * Used to return raw SQL query results.
 * 

* Refer to {@link SqlQuery} for examples. *

*

* There are convenience methods such as getInteger(), getBigDecimal() etc. The * reason for these methods is that the values put into this map often come * straight from the JDBC resultSet. Depending on the JDBC driver it may put a * different type into a given property. For example an Integer, BigDecimal, * Double could all be put into a property depending on the JDBC driver used. * These convenience methods automatically convert the value as required * returning the type you expect. *

*/ public class DefaultSqlRow implements SqlRow { static final long serialVersionUID = -3120927797041336242L; private final String dbTrueValue; /** * The underlying map of property data. */ Map map; /** * Create with a specific Map implementation. *

* The default Map implementation is LinkedHashMap. *

*/ public DefaultSqlRow(Map map, String dbTrueValue) { this.map = map; this.dbTrueValue = dbTrueValue; } /** * Create a new MapBean based on a LinkedHashMap with default * initialCapacity (of 16). */ public DefaultSqlRow(String dbTrueValue) { this.map = new LinkedHashMap(); this.dbTrueValue = dbTrueValue; } /** * Create with an initialCapacity and loadFactor. *

* The defaults of these are 16 and 0.75. *

*

* Note that the Map will rehash the contents when the number of keys in * this map reaches its threshold (initialCapacity * loadFactor). *

*/ public DefaultSqlRow(int initialCapacity, float loadFactor, String dbTrueValue) { this.map = new LinkedHashMap(initialCapacity, loadFactor); this.dbTrueValue = dbTrueValue; } public Iterator keys() { return map.keySet().iterator(); } public Object remove(Object name) { name = ((String) name).toLowerCase(); return map.remove(name); } public Object get(Object name) { name = ((String) name).toLowerCase(); return map.get(name); } public Object put(String name, Object value) { return setInternal(name, value); } public Object set(String name, Object value) { return setInternal(name, value); } private Object setInternal(String name, Object newValue) { // MapBean properties are always lowercase name = name.toLowerCase(); // valueList = null; return map.put(name, newValue); } public UUID getUUID(String name) { Object val = get(name); return BasicTypeConverter.toUUID(val); } public Boolean getBoolean(String name) { Object val = get(name); return BasicTypeConverter.toBoolean(val, dbTrueValue); } public Integer getInteger(String name) { Object val = get(name); return BasicTypeConverter.toInteger(val); } public BigDecimal getBigDecimal(String name) { Object val = get(name); return BasicTypeConverter.toBigDecimal(val); } public Long getLong(String name) { Object val = get(name); return BasicTypeConverter.toLong(val); } public Double getDouble(String name) { Object val = get(name); return BasicTypeConverter.toDouble(val); } public Float getFloat(String name) { Object val = get(name); return BasicTypeConverter.toFloat(val); } public String getString(String name) { Object val = get(name); return BasicTypeConverter.toString(val); } public java.util.Date getUtilDate(String name) { Object val = get(name); return BasicTypeConverter.toUtilDate(val); } public Date getDate(String name) { Object val = get(name); return BasicTypeConverter.toDate(val); } public Timestamp getTimestamp(String name) { Object val = get(name); return BasicTypeConverter.toTimestamp(val); } public String toString() { return map.toString(); } // ------------------------------------ // Normal map methods... public void clear() { map.clear(); } public boolean containsKey(Object key) { key = ((String) key).toLowerCase(); return map.containsKey(key); } public boolean containsValue(Object value) { return map.containsValue(value); } public Set> entrySet() { return map.entrySet(); } public boolean isEmpty() { return map.isEmpty(); } public Set keySet() { return map.keySet(); } public void putAll(Map t) { map.putAll(t); } public int size() { return map.size(); } public Collection values() { return map.values(); } }