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

sirius.db.jdbc.Row Maven / Gradle / Ivy

Go to download

Provides a modern and highly flexible ORM and lightweight connectivity for JDBC, MongoDB, Redis, Elasticsearch.

There is a newer version: 7.4
Show newest version
/*
 * Made with all the love in the world
 * by scireum in Remshalden, Germany
 *
 * Copyright by scireum GmbH
 * http://www.scireum.de - [email protected]
 */

package sirius.db.jdbc;

import com.google.common.collect.Maps;
import sirius.kernel.commons.Strings;
import sirius.kernel.commons.Tuple;
import sirius.kernel.commons.Value;

import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * A small wrapper class to represent a result row.
 */
public class Row {

    protected Map> fields = new LinkedHashMap<>();

    /**
     * Returns all stored fields as map.
     *
     * @return the underlying map, containing all fields and values. Modifying this map will modify the row
     */
    @Nonnull
    public Collection> getFieldsList() {
        return fields.values();
    }

    /**
     * Returns the underlying row as map.
     *
     * @return a map containing all fields and values represented by this row
     * @deprecated use {@link #getFieldsList()} which honors the field names in terms of upper- and lowercase.
     */
    @Nonnull
    @Deprecated
    public Map getFields() {
        Map result = Maps.newTreeMap();
        for (Map.Entry> field : fields.entrySet()) {
            result.put(field.getKey(), field.getValue().getSecond());
        }
        return Collections.unmodifiableMap(result);
    }

    /**
     * Determines if a value for a given key is present.
     *
     * @param key the key to check for
     * @return true if a value for the given key is present (even if it is null),
     * false otherwise
     */
    public boolean hasValue(@Nonnull String key) {
        return fields.containsKey(key.toUpperCase());
    }

    /**
     * Returns the value associated with the given key.
     *
     * @param key the name of the field to retrieve
     * @return the value associated with the given key wrapped as {@link sirius.kernel.commons.Value}
     * @throws java.lang.IllegalArgumentException if an unknown column key is requested
     */
    @Nonnull
    public Value getValue(@Nonnull Object key) {
        String upperCaseKey = key.toString().toUpperCase();
        if (!fields.containsKey(upperCaseKey)) {
            throw new IllegalArgumentException(Strings.apply("Unknown column: %s in %s", upperCaseKey, this));
        }
        return Value.of(fields.get(upperCaseKey).getSecond());
    }

    /**
     * Returns a sub list which was stored for a given key.
     * 

* It is assumed that the object associated with the given key is a {@code List<Row>}. * * @param key the name of the field to retrieve * @return the list associated with the given key * @throws ClassCastException if the stored value isn't a list. null is handled gracefully */ @SuppressWarnings("unchecked") @Nonnull public List getSublist(@Nonnull Object key) { return (List) getValue(key).get(Collections.emptyList()); } /** * Stores a value for the given key. *

* Can be used to add computed values for further processing. * * @param key the key to bind the value to * @param value the value to be stored */ public void setValue(@Nonnull String key, Object value) { fields.put(key.toUpperCase(), Tuple.create(key, value)); } @Override public String toString() { return "Row [" + fields + "]"; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy