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

com.wizarius.orm.database.DatabaseStorage Maven / Gradle / Ivy

There is a newer version: 0.0.27.3
Show newest version
package com.wizarius.orm.database;

import com.wizarius.orm.database.exceptions.DBException;
import com.wizarius.orm.database.interfaces.DBEntity;
import com.wizarius.orm.database.postgres.PostgresEntityInitializer;

import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Vladyslav Shyshkin on 29.04.17.
 */
public class DatabaseStorage {
    private final Class clazz;
    protected final DBConnectionPool pool;
    // TODO придумать как тут написать друго entity Initializer
    private final PostgresEntityInitializer entityInitializer;

    public DatabaseStorage(DBConnectionPool pool, Class clazz) throws DBException {
        this.pool = pool;
        this.entityInitializer = new PostgresEntityInitializer(clazz, pool);
        this.clazz = clazz;
    }

    public DatabaseStorage(DBConnectionPool pool) throws DBException {
        this.pool = pool;
        this.clazz = (Class) ((ParameterizedType) getClass()
                .getGenericSuperclass()).getActualTypeArguments()[0];
        this.entityInitializer = new PostgresEntityInitializer(clazz, pool);
    }

    /**
     * Save connection entity
     *
     * @param entity DBEntity
     * @throws DBException on unable to save to database
     */
    public void save(T entity) throws DBException {
        entityInitializer.getInsertQuery().execute(entity);
    }

    /**
     * Get table name
     *
     * @return table name
     */
    public String getTableName() {
        return entityInitializer.fieldsMap.getTableName();
    }

    /**
     * Save connection entity and return entity new id
     *
     * @param entity entity id
     * @return id of saved entity
     * @throws DBException on unable to save data and get last insert id
     */
    public long saveAndGetLastInsertID(T entity) throws DBException {
        return entityInitializer.getInsertQuery().executeQueryWithKey(entity);
    }

    /**
     * Update entity
     *
     * @param entity DBEntity
     * @throws DBException on unable to update entity in database
     */
    public void update(T entity, int id) throws DBException {
        entityInitializer.getUpdateQuery().where("id", id).execute(entity);
    }

    /**
     * Update entity
     *
     * @param entity DBEntity
     * @throws DBException on unable to update entity in database
     */
    public void update(T entity, long id) throws DBException {
        entityInitializer.getUpdateQuery().where("id", id).execute(entity);
    }

    /**
     * Get entity by id
     *
     * @param id entity id
     * @throws DBException on unable to get by id
     */
    public DBEntity getByID(Object id) throws DBException {
        return entityInitializer.getSelectQuery().where("id", id, clazz).getOne();
    }

    /**
     * Delete entity by id
     *
     * @param id entity id
     * @throws DBException on unable to delete by id
     */
    public void deleteByID(long id) throws DBException {
        entityInitializer.getDeleteQuery().where("id", id).execute();
    }

    /**
     * Delete entity by id
     *
     * @param id entity id
     * @throws DBException on unable to delete byy id
     */
    public void deleteByID(int id) throws DBException {
        entityInitializer.getDeleteQuery().where("id", id).execute();
    }

    /**
     * Get all records in database
     *
     * @return array list of entities
     * @throws DBException on unable to get all
     */
    public List getAll() throws DBException {
        return entityInitializer.getSelectQuery().execute();
    }

    /**
     * Get all elements casted to generic
     *
     * @return list of casted elements to generic
     * @throws DBException on unable to execute query
     */
    public List getAllCasted() throws DBException {
        List execute = entityInitializer.getSelectQuery().execute();
        List result = new ArrayList<>();
        for (DBEntity entity : execute) {
            result.add((T) entity);
        }
        return result;
    }

    /**
     * Delete all records from table
     *
     * @throws DBException on unable to clear
     */
    public void clear() throws DBException {
        entityInitializer.getDeleteQuery().execute();
    }

    /**
     * Get count elements in table
     *
     * @return count elements in table
     * @throws DBException database exception
     */
    public long count() throws DBException {
        return entityInitializer.getSelectQuery().getCount();
    }

    /**
     * Get session
     *
     * @return session
     */
    public PostgresEntityInitializer getSession() {
        return entityInitializer;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy