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

com.github.chengyuxing.sql.Args Maven / Gradle / Ivy

Go to download

Light wrapper of JDBC, support ddl, dml, query, plsql/procedure/function, transaction and manage sql file.

There is a newer version: 9.0.2
Show newest version
package com.github.chengyuxing.sql;

import com.github.chengyuxing.common.MapExtends;
import com.github.chengyuxing.common.utils.ObjectUtil;

import java.util.Arrays;
import java.util.HashMap;
import java.util.function.Function;

/**
 * Simple sql args tool.
 *
 * @param  value type
 */
public final class Args extends HashMap implements MapExtends {
    /**
     * Constructs a new empty Args.
     */
    public Args() {
    }

    /**
     * Returns an empty Args.
     *
     * @param  value type
     * @return empty Args instance
     */
    public static  Args of() {
        return new Args<>();
    }

    /**
     * Returns an Args with generic initial value.
     *
     * @param k   key
     * @param v   value
     * @param  value type
     * @return generic Args instance
     */
    public static  Args of(String k, V v) {
        Args args = of();
        args.put(k, v);
        return args;
    }

    /**
     * Returns an Args from more than one pairs of value.
     *
     * @param input key-value pairs: k v, k v, ...
     * @return Args instance
     */
    public static Args of(Object... input) {
        return ObjectUtil.pairs2map(i -> Args.of(), input);
    }

    /**
     * Returns an Args from standard java bean entity.
     *
     * @param entity standard java bean.
     * @return Args instance
     */
    public static Args ofEntity(Object entity) {
        return ObjectUtil.entity2map(entity, i -> Args.of());
    }

    /**
     * Add a key-value.
     *
     * @param k key
     * @param v value
     * @return Args instance
     */
    public Args add(String k, V v) {
        put(k, v);
        return this;
    }

    /**
     * Update a key name to another.
     *
     * @param oldKey old key name
     * @param newKey new key name
     */
    public void updateKey(String oldKey, String newKey) {
        if (containsKey(oldKey)) {
            put(newKey, remove(oldKey));
        }
    }

    /**
     * Update all key name.
     *
     * @param updater (old key name) -> (new key name)
     */
    public void updateKeys(Function updater) {
        Object[] keys = keySet().toArray();
        for (Object key : keys) {
            if (key == null) {
                continue;
            }
            String newKey = updater.apply(key.toString());
            put(newKey, remove(key));
        }
        Arrays.fill(keys, null);
    }
}