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

org.simpleflatmapper.jdbc.impl.LazyCrud Maven / Gradle / Ivy

Go to download

Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.

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

import org.simpleflatmapper.jdbc.Crud;
import org.simpleflatmapper.jdbc.CrudDSL;
import org.simpleflatmapper.jdbc.SelectQuery;
import org.simpleflatmapper.util.CheckedConsumer;

import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicReference;

public class LazyCrud implements Crud {

    private final CrudDSL crudDSL;
    private final AtomicReference> delegate = new AtomicReference>();
    private final String table;

    public LazyCrud(CrudDSL crudDSL, String table) {
        this.crudDSL = crudDSL;
        this.table = table;
    }

    private Crud getDelegate(Connection connection) throws SQLException {
        Crud crud;
        do {
            crud = delegate.get();

            if (crud != null)
                break;

            Crud newCrud = instantiateCrud(connection);
            if (delegate.compareAndSet(null, newCrud)) {
                crud = newCrud;
                break;
            }
        } while(true);
        return crud;
    }

    private Crud instantiateCrud(Connection connection) throws SQLException {
        if (table == null) {
            return crudDSL.to(connection);
        } else {
            return crudDSL.table(connection, table);
        }
    }

    @Override
    public void create(Connection connection, T value) throws SQLException {
        getDelegate(connection).create(connection, value);
    }

    @Override
    public void create(Connection connection, Collection values) throws SQLException {
        getDelegate(connection).create(connection, values);
    }

    @Override
    public > RH create(Connection connection, T value, RH keyConsumer) throws SQLException {
        return getDelegate(connection).create(connection, value, keyConsumer);
    }

    @Override
    public > RH create(Connection connection, Collection values, RH keyConsumer) throws SQLException {
        return getDelegate(connection).create(connection, values, keyConsumer);
    }

    @Override
    public T read(Connection connection, K key) throws SQLException {
        return getDelegate(connection).read(connection, key);
    }

    @Override
    public > RH read(Connection connection, Collection keys, RH consumer) throws SQLException {
        return getDelegate(connection).read(connection, keys, consumer);
    }

    @Override
    public void update(Connection connection, T value) throws SQLException {
        getDelegate(connection).update(connection, value);
    }

    @Override
    public void update(Connection connection, Collection values) throws SQLException {
        getDelegate(connection).update(connection, values);
    }

    @Override
    public void delete(Connection connection, K key) throws SQLException {
        getDelegate(connection).delete(connection, key);
    }

    @Override
    public void delete(Connection connection, Collection keys) throws SQLException {
        getDelegate(connection).delete(connection, keys);
    }

    @Override
    public void createOrUpdate(Connection connection, T value) throws SQLException {
        getDelegate(connection).createOrUpdate(connection, value);
    }

    @Override
    public void createOrUpdate(Connection connection, Collection values) throws SQLException {
        getDelegate(connection).createOrUpdate(connection, values);
    }

    @Override
    public > RH createOrUpdate(Connection connection, T value, RH keyConsumer) throws SQLException {
        return getDelegate(connection).createOrUpdate(connection, value, keyConsumer);
    }

    @Override
    public > RH createOrUpdate(Connection connection, Collection values, RH keyConsumer) throws SQLException {
        return getDelegate(connection).createOrUpdate(connection, values, keyConsumer);
    }

    @Override
    public 

SelectQuery where(String whereClause, Type paramClass) { Crud crud = delegate.get(); if (crud != null) { return crud.where(whereClause, paramClass); } else { return new LazySelectQuery

(whereClause, paramClass); } } private class LazySelectQuery

implements SelectQuery { private final String whereClause; private final Type paramClass; private final AtomicReference> delegate = new AtomicReference>(); public LazySelectQuery(String whereClause, Type paramClass) { this.whereClause = whereClause; this.paramClass = paramClass; } @Override public T readFirst(Connection connection, P p) throws SQLException { return getDelegateQuery(connection).readFirst(connection, p); } @Override public > C read(Connection connection, P p, C consumer) throws SQLException { return getDelegateQuery(connection).read(connection, p, consumer); } private SelectQuery getDelegateQuery(Connection connection) throws SQLException { SelectQuery query; do { query = delegate.get(); if (query != null) break; SelectQuery newQuery = getDelegate(connection).where(whereClause, paramClass); if (this.delegate.compareAndSet(null, newQuery)) { query = newQuery; break; } } while (true); return query; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy