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

com.notronix.hibermate.impl.DBQueryImpl Maven / Gradle / Ivy

package com.notronix.hibermate.impl;

import com.notronix.hibermate.api.DBQuery;
import org.hibernate.Session;
import org.hibernate.query.NativeQuery;

import java.util.HashMap;
import java.util.Map;

import static com.notronix.albacore.ContainerUtils.thereAreOneOrMore;

public class DBQueryImpl implements DBQuery
{
    private String query;
    private Map params;

    public DBQueryImpl() {
    }

    public DBQueryImpl(String query) {
        this.query = query;
    }

    @Override
    public String getQuery() {
        return query;
    }

    @Override
    public void setQuery(String query) {
        this.query = query;
    }

    @Override
    public void setParameter(String name, Object value) {
        if (params == null) {
            params = new HashMap<>();
        }

        params.put(name, value);
    }

    @Override
    public Object getParameter(String name) {
        return params == null ? null : params.get(name);
    }

    @Override
    public  NativeQuery getQuery(Session session, Class resultType) {
        @SuppressWarnings("unchecked")
        NativeQuery sqlQuery = resultType == null ?
                session.createNativeQuery(query) : session.createNativeQuery(query, resultType);

        if (thereAreOneOrMore(params)) {
            for (Map.Entry entry : params.entrySet()) {
                sqlQuery.setParameter(entry.getKey(), entry.getValue());
            }
        }

        return sqlQuery;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy