Added javadoc

This commit is contained in:
Willem 2015-06-19 23:56:45 +02:00
parent f1f101fde3
commit 6ddd2c969b
44 changed files with 382 additions and 71 deletions

View file

@ -24,17 +24,36 @@ package net.forwardfire.tpquery.store.executor.jdbc;
import java.sql.SQLException;
/**
* Wrap the SQLException as RuntimeException
*
* @author Willem Cazander
* @version 1.0 Jan 14, 2015
*/
@SuppressWarnings("serial")
public class SQLExceptionRuntime extends RuntimeException {
public SQLExceptionRuntime(SQLException parent) {
protected SQLExceptionRuntime(SQLException parent) {
super(parent);
}
/**
* Wraps the SQLException to an SQLExceptionRuntime
* @param e The SQLException to wrap.
* @return The new SQLExceptionRuntime.
*/
public static SQLExceptionRuntime wrap(SQLException e) {
return new SQLExceptionRuntime(e);
}
/**
* Wraps and closes.
* note: this is needed because with try-with-resources the compiler produces unreachable bytecode.
*
* @param closeable The auto closeable to close.
* @param e The sql exception to wrap.
* @return The new SQLExceptionRuntime.
*/
public static SQLExceptionRuntime wrapAndClose(AutoCloseable closeable,SQLException e) {
return close(closeable,wrap(e));
}

View file

@ -32,8 +32,18 @@ import net.forwardfire.tpquery.TPQFactory;
import net.forwardfire.tpquery.store.executor.AbstractTPQExecutor;
import net.forwardfire.tpquery.store.executor.TPQExecutorContext;
/**
* The jdbc query executor.
*
* @author Willem Cazander
* @version 1.0 Jan 14, 2015
*/
public class TPQExecutorJdbc extends AbstractTPQExecutor<Connection,PreparedStatement> {
/**
* Created the jdbc query executor.
* @param context The executor context.
*/
public TPQExecutorJdbc(TPQExecutorContext context) {
super(context);
registrateStatementCreator(TPQFactory.StatementLanguage.SQL, (connection,statementText) -> {
@ -45,10 +55,23 @@ public class TPQExecutorJdbc extends AbstractTPQExecutor<Connection,PreparedStat
});
}
/**
* Short hand to parameter-less queries.
* @param connection The connection to create the prepared statement.
* @param queryName The query to execute.
* @return The executed prepared statement.
*/
public PreparedStatement execute(Connection connection,String queryName) {
return execute(connection, queryName, null);
}
/**
* Executes and prepared statement.
* @param connection The connection to create the prepared statement.
* @param queryName The query to execute.
* @param parameters The query parameters.
* @return The executed prepared statement.
*/
public PreparedStatement execute(Connection connection,String queryName,Map<String,Object> parameters) {
return execute(createPreparedStatement(connection, queryName, parameters));
}

View file

@ -32,21 +32,47 @@ import net.forwardfire.tpquery.TPQFactory;
import net.forwardfire.tpquery.store.executor.AbstractTPQExecutor;
import net.forwardfire.tpquery.store.executor.TPQExecutorContext;
/**
* The jpa query executor.
*
* @author Willem Cazander
* @version 1.0 Jan 14, 2015
*/
public class TPQExecutorJpa extends AbstractTPQExecutor<EntityManager,Query> {
private static final String HINT_TIMEOUT = "javax.persistence.query.timeout";
/**
* Created the jpa query executor.
* @param context The executor context.
*/
public TPQExecutorJpa(TPQExecutorContext context) {
super(context);
registrateStatementCreator(TPQFactory.StatementLanguage.HQL, (em,stmt) -> em.createQuery(stmt));
registrateStatementCreator(TPQFactory.StatementLanguage.SQL, (em,stmt) -> em.createNativeQuery(stmt));
}
/**
* Selects a List of object from the database.
* @param entityManager The entity manager to query.
* @param queryName The query name to execute.
* @param parameters The parameter for the query.
* @param <E> The object type.
* @return The List of objects.
*/
@SuppressWarnings("unchecked")
public <E> List<E> selectList(EntityManager entityManager,String queryName,Map<String,Object> parameters) {
return createPreparedStatement(entityManager, queryName, parameters).getResultList();
}
/**
* Selects a single object from the database.
* @param entityManager The entity manager to query.
* @param queryName The query name to execute.
* @param parameters The parameter for the query.
* @param <E> The object type.
* @return The object.
*/
@SuppressWarnings("unchecked")
public <E> E selectObject(EntityManager entityManager,String queryName,Map<String,Object> parameters) {
return (E)createPreparedStatement(entityManager, queryName, parameters).getSingleResult();

View file

@ -157,7 +157,9 @@ public final class TPQFactory {
public static final class StatementLanguage implements TPQConfigInitializer {
/** The name of the sql language. */
public static final String SQL = "SQL";
/** The name of the hql language. */
public static final String HQL = "HQL";
private StatementLanguage() {
@ -171,9 +173,13 @@ public final class TPQFactory {
}
public static final class StatementParameter implements TPQConfigInitializer {
/** The value type parameter. */
public static final String VALUE = "VALUE";
/** The list type parameter. */
public static final String LIST = "LIST";
/** The raw type parameter. */
public static final String RAW = "RAW";
/** The raw_null type parameter. */
public static final String RAW_NULL = "RAW_NULL";
private StatementParameter() {
@ -190,29 +196,51 @@ public final class TPQFactory {
public static final class ParameterValueType {
// java type
/** The class name of an Object. */
public static final String JAVA_OBJECT = Object.class.getName();
/** The class name of an BigDecimal. */
public static final String BIGDECIMAL = java.math.BigDecimal.class.getName();
/** The class name of an Boolean. */
public static final String BOOLEAN = Boolean.class.getName();
/** The class name of an Integer. */
public static final String INTEGER = Integer.class.getName();
/** The class name of an String. */
public static final String STRING = String.class.getName();
/** The class name of an Double. */
public static final String DOUBLE = Double.class.getName();
/** The class name of an Long. */
public static final String LONG = Long.class.getName();
/** The class name of an Float. */
public static final String FLOAT = Float.class.getName();
/** The class name of an Short. */
public static final String SHORT = Short.class.getName();
/** The class name of an Url. */
public static final String URL = java.net.URL.class.getName();
/** The class name of an byte array. */
public static final String BYTE_DATA = byte[].class.getName();
// jdbc types
/** The class name of an java.sql.Array. */
public static final String SQL_ARRAY = java.sql.Array.class.getName();
/** The class name of an java.sql.Blob. */
public static final String SQL_BLOB = java.sql.Blob.class.getName();
/** The class name of an java.sql.Clob. */
public static final String SQL_CLOB = java.sql.Clob.class.getName();
/** The class name of an java.sql.Date. */
public static final String SQL_DATE = java.sql.Date.class.getName();
/** The class name of an java.sql.NClob. */
public static final String SQL_NCLOB = java.sql.NClob.class.getName();
/** The class name of an java.sql.Ref. */
public static final String SQL_REF = java.sql.Ref.class.getName();
/** The class name of an java.sql.RowId. */
public static final String SQL_ROWID = java.sql.RowId.class.getName();
/** The class name of an java.sql.Struct. */
public static final String SQL_STRUCT = java.sql.Struct.class.getName();
/** The class name of an java.sql.SQLXML. */
public static final String SQL_XML = java.sql.SQLXML.class.getName();
/** The class name of an java.sql.Time. */
public static final String SQL_TIME = java.sql.Time.class.getName();
/** The class name of an java.sql.Timestamp. */
public static final String SQL_TIMESTAMP = java.sql.Timestamp.class.getName();
protected ParameterValueType() {
@ -349,14 +377,23 @@ public final class TPQFactory {
private static final String FUNCTION_ARGS_END = ")";
private static final String FUNCTION_ARGS_EMPTY = FUNCTION_ARGS_BEGIN+FUNCTION_ARGS_END;
/** Creates an object. */
public static final String CREATE_OBJECT = "createObject";
/** Creates an arraylist. */
public static final String CREATE_ARRAY_LIST = "createArrayList";
/** Creates an hashmap object. */
public static final String CREATE_HASH_MAP = "createHashMap";
/** Creates an arraylist of objects. */
public static final String CREATE_OBJECT_LIST = "createObjectList";
/** Wraps an stable value object in a value holder marker interface. */
public static final String CREATE_VALUE_HOLDER = "createValueHolder";
/** Creates an epoch long value. */
public static final String EPOCH = "epoch";
/** Creates an date object. */
public static final String DATE = "date";
/** Creates an time object. */
public static final String TIME = "time";
/** Creates an timestamp object. */
public static final String TIMESTAMP = "timestamp";
private ParameterValueFunction() {

View file

@ -27,12 +27,20 @@ import net.forwardfire.tpquery.model.TPQueryHint;
/**
* Abstract query node builder for the shared properties of the AbstractTPQueryNode.
*
*
* @param <P> The parent builder.
* @param <T> The object to build.
* @param <B> This builder.
* @author Willem Cazander
* @version 1.0 May 29, 2015
*/
public abstract class AbstractTPQueryNodeBuilder<P,T extends AbstractTPQueryNode,B> extends AbstractTPQBuilder<P,T,B> {
/**
* Creates an node builder.
* @param parent The parent builder.
* @param build The object to build.
*/
public AbstractTPQueryNodeBuilder(P parent, T build) {
super(parent, build);
}

View file

@ -39,6 +39,7 @@ import net.forwardfire.tpquery.statement.TPQStatementMarshaller;
/**
* The root config builder.
*
* @param <T> The object to build.
* @author Willem Cazander
* @version 1.0 May 22, 2015
*/

View file

@ -33,6 +33,8 @@ import net.forwardfire.tpquery.statement.TPQStatementPartText;
/**
* Builds queries.
*
* @param <P> The parent builder.
* @param <T> The object to build.
* @author Willem Cazander
* @version 1.0 May 29, 2015
*/

View file

@ -28,7 +28,9 @@ import net.forwardfire.tpquery.model.TPQueryParameter;
/**
* Builds query parameters.
*
*
* @param <P> The parent builder.
* @param <T> The object to build.
* @author Willem Cazander
* @version 1.0 May 29, 2015
*/

View file

@ -27,7 +27,8 @@ import net.forwardfire.tpquery.model.TPQuerySet;
/**
* Builds query set.
*
*
* @param <T> The object to build.
* @author Willem Cazander
* @version 1.0 May 29, 2015
*/

View file

@ -32,6 +32,7 @@ import net.forwardfire.tpquery.model.TPQuerySet;
/**
* Builds query set as tree.
*
* @param <T> The object to build.
* @author Willem Cazander
* @version 1.0 May 29, 2015
*/

View file

@ -36,6 +36,10 @@ public final class TPQConfigValidatorCheckNamePattern implements TPQConfigValida
private final String pattern;
/**
* Creates an name pattern checker.
* @param pattern The pattern to check the name against.
*/
public TPQConfigValidatorCheckNamePattern(String pattern) {
Validate.matchesPattern("", pattern);
this.pattern=pattern;

View file

@ -30,6 +30,10 @@ package net.forwardfire.tpquery.config.validate;
*/
public final class TPQConfigValidatorCheckParameterNamePattern extends AbstractTPQConfigValidatorParameterPattern {
/**
* Creates an name pattern checker.
* @param pattern The pattern to check the name against.
*/
public TPQConfigValidatorCheckParameterNamePattern(String pattern) {
super(pattern,p -> p.getName());
}

View file

@ -30,6 +30,10 @@ package net.forwardfire.tpquery.config.validate;
*/
public final class TPQConfigValidatorCheckParameterTypePattern extends AbstractTPQConfigValidatorParameterPattern {
/**
* Creates an type pattern checker.
* @param pattern The pattern to check the type against.
*/
public TPQConfigValidatorCheckParameterTypePattern(String pattern) {
super(pattern,p -> p.getType());
}

View file

@ -30,6 +30,10 @@ package net.forwardfire.tpquery.config.validate;
*/
public final class TPQConfigValidatorCheckParameterValueTypePattern extends AbstractTPQConfigValidatorParameterPattern {
/**
* Creates an value type pattern checker.
* @param pattern The pattern to check the value type against.
*/
public TPQConfigValidatorCheckParameterValueTypePattern(String pattern) {
super(pattern,p -> p.getValueType());
}

View file

@ -47,6 +47,9 @@ public abstract class AbstractTPQueryNode {
private Integer timeout = null;
private final List<TPQueryHint> queryHints;
/**
* Creates an query node.
*/
public AbstractTPQueryNode() {
queryHints = new ArrayList<>();
}

View file

@ -44,11 +44,18 @@ public final class TPQuery extends AbstractTPQueryNode {
private List<TPQStatement> queryParts;
private final List<TPQueryParameter> queryParameters;
/**
* Creates a query.
*/
public TPQuery() {
queryParts = new ArrayList<>();
queryParameters = new ArrayList<>();
}
/**
* Creates a query.
* @param name The name of the query.
*/
public TPQuery(String name) {
this();
setName(name);
@ -62,12 +69,17 @@ public final class TPQuery extends AbstractTPQueryNode {
public List<TPQStatement> getQueryParts() {
return queryParts;
}
/*
public void setQueryParts(List<TPQStatement> parts) {
Objects.requireNonNull(parts,"Can't set null list");
queryParts = parts;
}
*/
/**
* Adds an query part.
* @param queryPart The part to add.
*/
public void addQueryPart(TPQStatement queryPart) {
Objects.requireNonNull(queryPart,"Can't add null");
queryParts.add(queryPart);
@ -81,6 +93,10 @@ public final class TPQuery extends AbstractTPQueryNode {
return queryParameters;
}
/**
* Adds an queryParameter.
* @param queryParameter The parameter to add.
*/
public void addQueryParameter(TPQueryParameter queryParameter) {
Objects.requireNonNull(queryParameter,"Can't add null");
Objects.requireNonNull(queryParameter.getName(),"TQueryParameter.getName() is null.");

View file

@ -37,16 +37,20 @@ public final class TPQueryHint {
private String name = null;
private String value = null;
/**
* Creates a query hint.
*/
public TPQueryHint() {
}
public TPQueryHint(String name) {
/**
* Creates a query hint.
* @param name The name of the hint.
* @param value The value of the hint.
*/
public TPQueryHint(String name,String value) {
this();
setName(name);
}
public TPQueryHint(String name,String value) {
this(name);
setValue(value);
}

View file

@ -40,9 +40,17 @@ public final class TPQueryParameter {
private String valueType = null;
private Boolean nullable = null;
/**
* Creates a query parameter.
*/
public TPQueryParameter() {
}
/**
* Creates a query parameter.
* @param name The name of the query parameter.
* @param valueType The value type of the query parameter.
*/
public TPQueryParameter(String name,String valueType) {
setName(name);
setValueType(valueType);

View file

@ -42,11 +42,18 @@ public class TPQuerySet extends AbstractTPQueryNode {
private final List<TPQuerySet> querySets;
private final List<TPQuery> queries;
/**
* Creates a query set.
*/
public TPQuerySet() {
queries = new ArrayList<>();
querySets = new ArrayList<>();
}
/**
* Creates a query set.
* @param name The name of the query set.
*/
public TPQuerySet(String name) {
this();
setName(name);

View file

@ -41,15 +41,29 @@ public class TPQuerySetXMLMarshaller extends AbstractXMLMarshaller {
private final JAXBContext querySetContext;
/**
* Creates a query set xml marshaller.
* @throws JAXBException When context could not be build.
*/
public TPQuerySetXMLMarshaller() throws JAXBException {
this(JAXBContext.newInstance(TPQuerySet.class));
}
/**
* Creates a query set xml marshaller.
* @param querySetContext The jaxb query set context.
*/
public TPQuerySetXMLMarshaller(JAXBContext querySetContext) {
Objects.requireNonNull(querySetContext,"querySetContext is null.");
this.querySetContext = querySetContext;
}
/**
* Marshal the query set to xml.
* @param querySet The query set to marshal.
* @param output The xml output of the query set.
* @throws JAXBException When error.
*/
public void marshal(TPQuerySet querySet, OutputStream output) throws JAXBException {
Objects.requireNonNull(querySet,"querySet is null.");
Objects.requireNonNull(output,"OutputStream is null.");
@ -60,6 +74,12 @@ public class TPQuerySetXMLMarshaller extends AbstractXMLMarshaller {
marshaller.marshal(querySet, output);
}
/**
* Unmarshals xml to query set objects.
* @param input The xml input stream.
* @return The query set.
* @throws JAXBException When error.
*/
public TPQuerySet unmarshal(InputStream input) throws JAXBException {
Objects.requireNonNull(input,"InputStream is null.");

View file

@ -38,6 +38,10 @@ public abstract class AbstractTPQStatement implements TPQStatement {
private TPQStatementContext context;
private TPQuery query;
/**
* Creates an statement.
* @param statementPart The text value of the statement part.
*/
public AbstractTPQStatement(String statementPart) {
Validate.notNull(statementPart,"statementPart is null");
this.statementPart=statementPart;
@ -50,6 +54,9 @@ public abstract class AbstractTPQStatement implements TPQStatement {
prepareInit();
}
/**
* init this statement.
*/
protected void prepareInit() {
}

View file

@ -44,18 +44,30 @@ public abstract class AbstractTPQStatementWriter implements TPQStatementWriter {
protected static final String PARAMETER_NEXT = ",";
private static final int OUTPUT_START_SIZE = 384;
protected boolean flagValueBased = false;
protected final StringBuilder output;
protected final List<TPQueryStoreStatementMapper> queryParameterMapping;
protected final Map<String,Object> queryParameterData;
private final StringBuilder outputBuffer;
private final List<TPQueryStoreStatementMapper> queryParameterMapping;
private final Map<String,Object> queryParameterData;
/**
* Creates an writer.
* @param queryParameterData The parameter value data.
*/
public AbstractTPQStatementWriter(Map<String,Object> queryParameterData) {
Validate.notNull(queryParameterData);
this.queryParameterData=queryParameterData;
this.output = new StringBuilder(OUTPUT_START_SIZE);
this.outputBuffer = new StringBuilder(OUTPUT_START_SIZE);
this.queryParameterMapping = new ArrayList<>();
}
/**
* @return Returns the output buffer.
*/
protected StringBuilder getOutputBuffer() {
return outputBuffer;
}
// ------ TPQStatementWriter methods
@Override
public void writeQuery(TPQuery query) {
Validate.notNull(query,"Can' write null query ");
@ -75,19 +87,19 @@ public abstract class AbstractTPQStatementWriter implements TPQStatementWriter {
@Override
public void appendQueryText(String text) {
Validate.notNull(text, "Can't add null text.");
output.append(text);
outputBuffer.append(text);
}
@Override
public void appendQueryParameter(TPQueryStoreStatementMapper mapper) {
Validate.notNull(mapper);
queryParameterMapping.add(mapper);
output.append(PARAMETER_PREPARED);
outputBuffer.append(PARAMETER_PREPARED);
}
@Override
public void appendQueryParameterSeperator() {
output.append(PARAMETER_NEXT);
outputBuffer.append(PARAMETER_NEXT);
}
@Override
@ -97,6 +109,6 @@ public abstract class AbstractTPQStatementWriter implements TPQStatementWriter {
@Override
public String getQueryText() {
return output.toString();
return outputBuffer.toString();
}
}

View file

@ -40,6 +40,11 @@ public class TPQStatementMarshaller {
protected static final String INCLUDE_PREFIX = "inc:";
private static final int DEFAULT_PRINT_SIZE = 384;
/**
* Marshal statement parts list as text.
* @param parts The pasts to marshal.
* @return The statements as text.
*/
public String marshal(List<TPQStatement> parts) {
Objects.requireNonNull(parts,"Can't print null list.");
StringBuilder buf = new StringBuilder(DEFAULT_PRINT_SIZE);
@ -60,10 +65,15 @@ public class TPQStatementMarshaller {
return buf.toString();
}
public List<TPQStatement> unmarshal(String statementTemplateText) {
Objects.requireNonNull(statementTemplateText,"Can't parse null text.");
/**
* Unmarshal the statement text into statement parts.
* @param statementText The statement text to parse.
* @return The list with statement parts.
*/
public List<TPQStatement> unmarshal(String statementText) {
Objects.requireNonNull(statementText,"Can't parse null text.");
List<TPQStatement> result = new ArrayList<>();
parseLoop(result,statementTemplateText);
parseLoop(result,statementText);
return result;
}

View file

@ -42,7 +42,11 @@ public interface TPQStatementWriter {
*/
void writeQuery(TPQuery query);
/**
* Gets an parameter value of the parameter dasta.
* @param parameter The parameter to get the value for.
* @return The query parameter value.
*/
Object getParameterValue(TPQueryParameter parameter);
/**

View file

@ -56,7 +56,7 @@ public class TPQStatementLanguageHql extends AbstractTPQStatementLanguage {
@Override
public void appendQueryParameter(TPQueryStoreStatementMapper valueMapper) {
super.appendQueryParameter(valueMapper);
output.append(this.queryParameterMapping.size());
getOutputBuffer().append(getQueryParameterMapping().size());
}
}
}

View file

@ -1,5 +1,5 @@
/**
* Query statements are written from parts: TEXT,PARAMETER,INCLUDE
* Query statements are written from parts: TEXT,PARAMETER,INCLUDE.
*
* @author Willem Cazander
*/

View file

@ -1,5 +1,5 @@
/**
* Statement parameter types: VALUE,LIST,RAW
* Statement parameter types: VALUE,LIST,RAW.
*
* @author Willem Cazander
*/

View file

@ -47,42 +47,69 @@ public class TPQueryStoreStatement implements Serializable {
private String statement;
private String language;
private Integer timeout;
private Map<String,String> properties;
private Map<String,String> queryHints;
private List<TPQueryStoreStatementMapper> valueMapping;
public TPQueryStoreStatement(String name,String statement,String language,Integer timeout,Map<String,String> properties,List<TPQueryStoreStatementMapper> valueMapping) {
/**
* Creates a query store statement.
* @param name The name of the statement.
* @param statement The text of the statement.
* @param language The language of the statement.
* @param timeout The timeout.
* @param queryHints The query hints.
* @param valueMapping The value mapping of the statement.
*/
public TPQueryStoreStatement(String name,String statement,String language,Integer timeout,Map<String,String> queryHints,List<TPQueryStoreStatementMapper> valueMapping) {
Objects.requireNonNull(name,"name is null");
Objects.requireNonNull(statement,"statement is null");
Objects.requireNonNull(language,"language is null");
Objects.requireNonNull(properties,"properties is null");
Objects.requireNonNull(queryHints,"properties is null");
this.name=name;
this.statement=statement;
this.timeout=timeout; // null is no timeout
this.language=language;
this.properties=Collections.unmodifiableMap(properties);
this.queryHints=Collections.unmodifiableMap(queryHints);
this.valueMapping=Collections.unmodifiableList(valueMapping);
}
/**
* @return The name of the statement.
*/
public String getName() {
return name;
}
/**
* @return The statement text.
*/
public String getStatement() {
return statement;
}
/**
* @return The statement langauge.
*/
public String getLanguage() {
return language;
}
/**
* @return The statement timeout.
*/
public Integer getTimeout() {
return timeout;
}
public Map<String,String> getProperties() {
return properties;
/**
* @return The statement query hints.
*/
public Map<String,String> getQueryHints() {
return queryHints;
}
/**
* @return The value mappers.
*/
public List<TPQueryStoreStatementMapper> getValueMapping() {
return valueMapping;
}
@ -94,7 +121,7 @@ public class TPQueryStoreStatement implements Serializable {
out.writeUTF(statement);
out.writeUTF(language);
out.writeObject(timeout);
out.writeObject(new HashMap<>(properties));
out.writeObject(new HashMap<>(queryHints));
out.writeObject(new ArrayList<>(valueMapping));
}
@ -104,7 +131,7 @@ public class TPQueryStoreStatement implements Serializable {
statement = in.readUTF();
language = in.readUTF();
timeout = (Integer)in.readObject();
properties = Collections.unmodifiableMap((Map<String, String>) in.readObject());
queryHints = Collections.unmodifiableMap((Map<String, String>) in.readObject());
valueMapping = Collections.unmodifiableList((List<TPQueryStoreStatementMapper>) in.readObject());
}

View file

@ -28,12 +28,23 @@ import java.util.Map;
import java.util.Objects;
import java.util.function.BiFunction;
/**
* The abstract query executor make implementing an executor easier.
*
* @author Willem Cazander
* @version 1.0 Feb 19, 2015
*/
public abstract class AbstractTPQExecutor<C,S> {
private static final int PARAMETER_INDEX_START = 1;
private final TPQExecutorContext context;
private final Map<String,BiFunction<C,String,S>> backendTypes;
/**
* Creates the executor with a context.
* @param context The executor context.
*/
public AbstractTPQExecutor(TPQExecutorContext context) {
Objects.requireNonNull(context,"Can't execute with null context.");
this.context = context;
@ -46,18 +57,41 @@ public abstract class AbstractTPQExecutor<C,S> {
backendTypes.put(language, statementCreator);
}
/**
* @return The executor context.
*/
protected final TPQExecutorContext getContext() {
return context;
}
/**
* Creates an backend statement.
* @param connection The backend connection to create the statement for.
* @param queryName The query of the statement.
* @param parameters The query parameters.
* @return The backend statement.
*/
public S createPreparedStatement(C connection,String queryName,Map<String,Object> parameters) {
return prepare(connection,getContext().prepareQuery(queryName,parameters));
}
/**
* Executes an update without parameters.
* @param connection The backend connection to execute on.
* @param queryName The query to execute.
* @return The number of records updates.
*/
public int executeUpdate(C connection,String queryName) {
return executeUpdate(connection, queryName, null);
}
/**
* Executes an update.
* @param connection The backend connection to execute on.
* @param queryName The query to execute.
* @param parameters The query parameters.
* @return The number of records updates.
*/
public int executeUpdate(C connection,String queryName,Map<String,Object> parameters) {
return executeUpdate(createPreparedStatement(connection, queryName, parameters));
}

View file

@ -26,22 +26,40 @@ import java.util.List;
import java.util.Map;
/**
*
* Statement for the executor.
*
* @author Willem Cazander
* @version 1.0 Jan 14, 2015
*/
public interface TPQExecutorStatement {
/**
* @return The name of the statement.
*/
String getName();
/**
* @return The statement text.
*/
String getStatement();
/**
* @return The parameters in ordered state.
*/
List<Object> getParameters();
/**
* @return The timeout of the request.
*/
Integer getTimeout();
/**
* @return The language of the statement text.
*/
String getLanguage();
Map<String,String> getProperties();
/**
* @return The query hints.
*/
Map<String,String> getQueryHints();
}

View file

@ -174,7 +174,7 @@ public final class TPQStoreManagerConfig extends AbstractTPQConfig {
LOG.debug("Created store entry for query: {} systemId: {}",entry.getQuery().getName(),entry.getSystemId());
q.getQueryHints().forEach(hint -> {
entry.getNamedProperties().put(hint.getName(), hint.getValue());
entry.getQueryHints().put(hint.getName(), hint.getValue());
LOG.trace("Mapped query hint name: {} value {}",hint.getName(),hint.getValue());
});

View file

@ -52,7 +52,7 @@ public final class TPQStoreManagerEntry {
private final String systemId;
private boolean valueMappedWriter = true;
private boolean validateParameters = true;
private final Map<String,String> namedProperties;
private final Map<String,String> queryHints;
private final Map<String,CompiledScript> parameterDefaultCompiledScripts;
private final Map<String,TPQConfigParameterValueHolder> parameterDefaultValues;
private Map<String,TPQueryParameterMetaData> parameterMetaData;
@ -68,7 +68,7 @@ public final class TPQStoreManagerEntry {
Objects.requireNonNull(systemId);
this.query=query;
this.systemId=systemId;
this.namedProperties=new HashMap<>();
this.queryHints=new HashMap<>();
this.parameterDefaultCompiledScripts=new HashMap<>();
this.parameterDefaultValues=new HashMap<>();
for (TPQueryHint hint:query.getQueryHints()) {
@ -123,8 +123,8 @@ public final class TPQStoreManagerEntry {
/**
* @return the properties
*/
public Map<String, String> getNamedProperties() {
return namedProperties;
public Map<String, String> getQueryHints() {
return queryHints;
}
/**

View file

@ -43,6 +43,8 @@ public class TPQStoreManagerEntryParameterMetaData implements TPQueryParameterMe
* Create an info for the parameter.
* @param name The parameter name.
* @param valueType The parameter type.
* @param nullable The nullable info the of parameter.
* @param required The required info the of parameter.
*/
public TPQStoreManagerEntryParameterMetaData(String name,Class<?> valueType,boolean nullable,boolean required) {
Objects.requireNonNull(name);

View file

@ -27,6 +27,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import javax.script.CompiledScript;
@ -77,7 +78,8 @@ public class TPQStoreManagerExecutorContext implements TPQExecutorContext {
public TPQExecutorStatement prepareQuery(String queryName,Map<String,Object> para) {
TPQStoreManagerEntry entry = config.getEntries().get(queryName);
Validate.notNull(entry, "Could not find query: %s",queryName);
return preparedQueryEntry(entry, para!=null?para:new HashMap<>());
Map<String,Object> paraMap = new HashMap<>(Optional.ofNullable(para).orElse(new HashMap<>()));
return preparedQueryEntry(entry, paraMap);
}
private TPQExecutorStatement preparedQueryEntry(TPQStoreManagerEntry entry,Map<String,Object> para) {
@ -217,7 +219,7 @@ public class TPQStoreManagerExecutorContext implements TPQExecutorContext {
qw.getQueryText(),
me.getQuery().getLanguage(),
me.getQuery().getTimeout(),
me.getNamedProperties(),
me.getQueryHints(),
qw.getQueryParameterMapping()
);
}

View file

@ -79,7 +79,7 @@ public final class TPQStoreManagerExecutorStatement implements TPQExecutorStatem
}
@Override
public Map<String,String> getProperties() {
return statement.getProperties();
public Map<String,String> getQueryHints() {
return statement.getQueryHints();
}
}

View file

@ -42,7 +42,7 @@ public class TPQStoreManagerStatementCache implements TPQueryStoreStatementCache
/**
* The default cache map initial capacity (MUST be a power of two)
*/
private static final int MAP_INITIAL_CAPACITY = 1 << 6; // aka 64
private static final int MAP_INITIAL_CAPACITY = 64;
/**
* The cache data.

View file

@ -40,9 +40,8 @@ public @interface TPQueryName {
/**
* Sets an non default query name.
* It defaults to the method name.
* note: result is formatted with the method name as 1st parameter.
*
* @return The query name.
* note: result is formatted with the method name as 1st parameter.
* @return The query name.
*/
String value() default "%s";
}

View file

@ -38,7 +38,8 @@ import java.lang.annotation.Target;
public @interface TPQueryParameterName {
/**
* @return The query parameter name.
* The query parameter name.
* @return The query parameter name.
*/
String value();
}

View file

@ -41,8 +41,7 @@ public @interface TPQueryProxy {
* Sets an non default query prefix.
* It defaults to an empty string.
* note: result is formatted with the interface simple class name as 1st parameter.
*
* @return The prefix of the queries in the interface of the method.
* @return The query prefix.
*/
String prefix() default "";
}

View file

@ -73,6 +73,7 @@ public class TPQueryProxyFactory {
/**
* Creates an new proxy instance for the query interface class.
*
* @param <T> The type of the returned instance.
* @param queryInterface The class to make a query proxy for.
* @return The wired proxy.
*/
@ -98,8 +99,12 @@ public class TPQueryProxyFactory {
TPQueryProxy queryProxy = Validate.notNull(queryInterface.getAnnotation(TPQueryProxy.class));
String queryPrefix = String.format(queryProxy.prefix(),queryInterface.getSimpleName());
for (Method method:queryInterface.getMethods()) {
String queryNamePart = method.getName();
TPQueryName query = method.getAnnotation(TPQueryName.class);
String queryName = queryPrefix + (query!=null?String.format(query.value(),method.getName()):method.getName());
if (query!=null) {
queryNamePart = String.format(query.value(),method.getName());
}
String queryName = queryPrefix + queryNamePart;
BiFunction<String,Map<String,Object>,Object> handler = lookupInvokeHandler(method.getReturnType());
Function<Map<String,Object>,Object> invoker = parameters -> handler.apply(queryName, parameters);
methodInvokers.put(method, invoker);

View file

@ -113,15 +113,12 @@ public class TPQSpeedTest {
.build();
// warmup
Map<String,Object> para1 = new HashMap<>(); // FIXME: ??? defaults are adding in this maps
Map<String,Object> para2 = new HashMap<>();
Map<String,Object> para3 = new HashMap<>();
Map<String,Object> para4 = new HashMap<>();
Map<String,Object> para = new HashMap<>();
for (int i=0;i<warmupSize;i++) {
queryManager.getQueryExecutorContext().prepareQuery("junit.speed-prepared", para1);
queryManager.getQueryExecutorContext().prepareQuery("junit.speed-raw", para2);
queryManager.getQueryExecutorContext().prepareQuery("junit.speed-function-dynamic", para3);
queryManager.getQueryExecutorContext().prepareQuery("junit.speed-function-holded", para4);
queryManager.getQueryExecutorContext().prepareQuery("junit.speed-prepared", para);
queryManager.getQueryExecutorContext().prepareQuery("junit.speed-raw", para);
queryManager.getQueryExecutorContext().prepareQuery("junit.speed-function-dynamic", para);
queryManager.getQueryExecutorContext().prepareQuery("junit.speed-function-holded", para);
}
}

View file

@ -158,7 +158,7 @@ public class TPQueryStoreConfigTest {
assertNotNull(q.getTimeout());
assertNotNull(q.getStatement());
assertNotNull(q.getParameters());
assertNotNull(q.getProperties());
assertNotNull(q.getQueryHints());
}
@Test
@ -234,13 +234,13 @@ public class TPQueryStoreConfigTest {
.build();
assertNotNull(store);
TPQExecutorStatement q = store.getQueryExecutorContext().prepareQuery("junit.test", null);
assertNotNull(q.getProperties().get("p0"));
assertEquals("v0",q.getProperties().get("p0"));
assertEquals("v1",q.getProperties().get("p1"));
assertNotNull(q.getQueryHints().get("p0"));
assertEquals("v0",q.getQueryHints().get("p0"));
assertEquals("v1",q.getQueryHints().get("p1"));
TPQExecutorStatement qvv = store.getQueryExecutorContext().prepareQuery("junit.test-vv", null);
assertNotNull(qvv.getProperties().get("p0"));
assertEquals("vv",qvv.getProperties().get("p0"));
assertEquals("v1",qvv.getProperties().get("p1"));
assertNotNull(qvv.getQueryHints().get("p0"));
assertEquals("vv",qvv.getQueryHints().get("p0"));
assertEquals("v1",qvv.getQueryHints().get("p1"));
}
}

View file

@ -60,7 +60,7 @@ public class TQueryExecutorTest {
}
@Override
public Map<String, String> getProperties() {
public Map<String, String> getQueryHints() {
return Collections.emptyMap();
}
@ -98,7 +98,7 @@ public class TQueryExecutorTest {
}
@Override
public Map<String, String> getProperties() {
public Map<String, String> getQueryHints() {
return Collections.emptyMap();
}

View file

@ -26,7 +26,7 @@ public class TPQueryPropertyTest {
.build();
Map<String,Object> para = new HashMap<String,Object>();
TPQExecutorStatement prepared = store.getQueryExecutorContext().prepareQuery("junit.test", para);
assertEquals("v0",prepared.getProperties().get("p0"));
assertEquals("v0",prepared.getQueryHints().get("p0"));
}
}