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

com.jaregu.database.queries.ext.guice.QueriesModule Maven / Gradle / Ivy

Go to download

Java based SQL templating project. Store your queries in *.sql files and build queries for execution. Supports simple expressions and conditional clauses and interface proxying for java-sql bridge.

There is a newer version: 1.4.1
Show newest version
package com.jaregu.database.queries.ext.guice;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Provider;
import javax.inject.Singleton;

import com.google.inject.AbstractModule;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.TypeLiteral;
import com.google.inject.multibindings.MapBinder;
import com.google.inject.multibindings.Multibinder;
import com.jaregu.database.queries.Queries;
import com.jaregu.database.queries.compiling.Entities;
import com.jaregu.database.queries.parsing.QueriesSource;
import com.jaregu.database.queries.parsing.QueriesSources;
import com.jaregu.database.queries.parsing.Sources;

public class QueriesModule {

	private QueriesModule() {
	}

	public static Module queriesModule() {
		return new AbstractModule() {
			@Override
			protected void configure() {
				// MME called at least one time for guice to create empty list if no source is supplied
				Multibinder.newSetBinder(binder(), QueriesSource.class);

				// MME called at least one time for guice to create empty list if no configurator is supplied
				Multibinder.newSetBinder(binder(), QueriesConfigurator.class);

				// MME called at least one time for guice to create empty list if no entity class is registered
				MapBinder.newMapBinder(binder(),
						new TypeLiteral() {
						}, new TypeLiteral>() {
						});

				TypeLiteral> sourcesType = new TypeLiteral>() {
				};
				TypeLiteral>> entitiesType = new TypeLiteral>>() {
				};
				TypeLiteral> configuratorType = new TypeLiteral>() {
				};
				Provider> sourcesProvider = getProvider(Key.get(sourcesType));
				Provider>> entitiesProvider = getProvider(Key.get(entitiesType));
				Provider> configuratorsProvider = getProvider(Key.get(configuratorType));

				bind(Queries.class).toProvider(() -> {
					Queries.Builder queriesBuilder = Queries
							.builder()
							.sources(sourcesProvider.get());

					entitiesProvider.get()
							.entrySet()
							.forEach(e -> queriesBuilder.entity(e.getValue(), e.getKey()));

					configuratorsProvider.get().forEach(c -> {
						c.configure(queriesBuilder);
					});

					return queriesBuilder.build();
				}).in(Singleton.class);
			}
		};
	}

	public static Module queriesConfiguratorModule(Class configuratorClass) {
		return new AbstractModule() {
			@Override
			protected void configure() {
				Multibinder sourcesBinder = Multibinder.newSetBinder(binder(),
						QueriesConfigurator.class);
				sourcesBinder.addBinding().to(configuratorClass);
			}
		};
	}

	public static Module queriesConfiguratorModule(QueriesConfigurator configurator) {
		return new AbstractModule() {
			@Override
			protected void configure() {
				Multibinder sourcesBinder = Multibinder.newSetBinder(binder(),
						QueriesConfigurator.class);
				sourcesBinder.addBinding().toInstance(configurator);
			}
		};
	}

	public static SourceModuleBuilder sourceModuleBuilder() {
		return new SourceModuleBuilder();
	}

	public static Module sourceModule(QueriesSource... sources) {
		return sourceModuleBuilder().sources(sources).build();
	}

	/**
	 * Module to add {@link Entities#entity(Class)}
	 * 
	 * @param entityClass
	 * @return
	 */
	public static  Module entityModule(Class entityClass) {
		return entityModule(entityClass, entityClass.getSimpleName());
	}

	/**
	 * Module to add {@link Entities#entity(Class, String)}
	 * 
	 * @param entityClass
	 * @param alias
	 * @return
	 */
	public static  Module entityModule(Class entityClass, String alias) {

		return new AbstractModule() {
			@Override
			protected void configure() {
				MapBinder> entitiesBinder = MapBinder.newMapBinder(binder(),
						new TypeLiteral() {
						}, new TypeLiteral>() {
						});

				entitiesBinder.addBinding(alias).toInstance(entityClass);
			}
		};
	}

	public static  Module proxyModule(Class proxyInterface) {

		return new AbstractModule() {
			@Override
			protected void configure() {
				Provider queriesProvider = getProvider(Queries.class);

				Multibinder sourcesBinder = Multibinder.newSetBinder(binder(), QueriesSource.class);
				sourcesBinder.addBinding().toInstance(QueriesSource.ofClass(proxyInterface));

				bind(proxyInterface).toProvider(new ProxyInterfaceProvider(queriesProvider, proxyInterface))
						.in(Singleton.class);
			}
		};
	}

	@SafeVarargs
	public static Module sourceClassesModule(Class... sources) {
		return new AbstractModule() {
			@Override
			protected void configure() {
				Multibinder sourcesBinder = Multibinder.newSetBinder(binder(), QueriesSource.class);
				for (Class source : sources) {
					sourcesBinder.addBinding().to(source);
				}
			}
		};
	}

	public static class SourceModuleBuilder implements Sources {

		private List sources = new LinkedList<>();

		@Override
		public SourceModuleBuilder sources(QueriesSources sources) {
			this.sources.addAll(sources.getSources());
			return this;
		}

		public Module build() {
			return new AbstractModule() {
				@Override
				protected void configure() {
					Multibinder sourcesBinder = Multibinder.newSetBinder(binder(), QueriesSource.class);
					for (QueriesSource source : sources) {
						sourcesBinder.addBinding().toInstance(source);
					}
				}
			};
		}
	}

	private static class ProxyInterfaceProvider implements Provider {

		private Provider queriesProvider;
		private Class proxyInterface;

		ProxyInterfaceProvider(Provider queriesProvider, Class proxyInterface) {
			this.queriesProvider = queriesProvider;
			this.proxyInterface = proxyInterface;
		}

		@Override
		public T get() {
			return queriesProvider.get().proxy(proxyInterface);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy