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

org.simpleflatmapper.jdbc.spring.JdbcTemplateCrudDSL Maven / Gradle / Ivy

There is a newer version: 9.0.2
Show newest version
package org.simpleflatmapper.jdbc.spring;

import org.simpleflatmapper.jdbc.Crud;
import org.simpleflatmapper.jdbc.JdbcMapperFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcOperations;

import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.SQLException;

public class JdbcTemplateCrudDSL {
    private final Type keyTarget;
    private final Type target;
    private final JdbcTemplateMapperFactory jdbcTemplateMapperFactory;

    public JdbcTemplateCrudDSL(JdbcTemplateMapperFactory jdbcTemplateMapperFactory, Type target, Type keyTarget) {
        this.jdbcTemplateMapperFactory = jdbcTemplateMapperFactory;
        this.target = target;
        this.keyTarget = keyTarget;
    }

    public JdbcTemplateCrud to(JdbcOperations jdbcOperations, final String table) {
        final JdbcMapperFactory factory = JdbcMapperFactory.newInstance(jdbcTemplateMapperFactory);

        Crud crud =
            jdbcOperations.execute(new ConnectionCallback>() {
                @Override
                public Crud doInConnection(Connection connection) throws SQLException, DataAccessException {
                    return factory.crud(target, keyTarget).table(connection, table);
                }
            });

        return new JdbcTemplateCrud(jdbcOperations, crud);
    }

    public JdbcTemplateCrud to(JdbcOperations jdbcOperations) {
        final JdbcMapperFactory factory = JdbcMapperFactory.newInstance(jdbcTemplateMapperFactory);

        Crud crud =
                jdbcOperations.execute(new ConnectionCallback>() {
                    @Override
                    public Crud doInConnection(Connection connection) throws SQLException, DataAccessException {
                        return factory.crud(target, keyTarget).to(connection);
                    }
                });

        return new JdbcTemplateCrud(jdbcOperations, crud);
    }


    public JdbcTemplateCrud lazilyTo(JdbcOperations jdbcOperations, final String table) {
        final JdbcMapperFactory factory = JdbcMapperFactory.newInstance(jdbcTemplateMapperFactory);
        Crud crud = factory.crud(target, keyTarget).table(table);
        return new JdbcTemplateCrud(jdbcOperations, crud);
    }

    public JdbcTemplateCrud lazilyTo(JdbcOperations jdbcOperations) {
        final JdbcMapperFactory factory = JdbcMapperFactory.newInstance(jdbcTemplateMapperFactory);
        Crud crud = factory.crud(target, keyTarget).crud();
        return new JdbcTemplateCrud(jdbcOperations, crud);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy