Added javadoc
This commit is contained in:
parent
f1f101fde3
commit
6ddd2c969b
|
@ -24,17 +24,36 @@ package net.forwardfire.tpquery.store.executor.jdbc;
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrap the SQLException as RuntimeException
|
||||||
|
*
|
||||||
|
* @author Willem Cazander
|
||||||
|
* @version 1.0 Jan 14, 2015
|
||||||
|
*/
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
public class SQLExceptionRuntime extends RuntimeException {
|
public class SQLExceptionRuntime extends RuntimeException {
|
||||||
|
|
||||||
public SQLExceptionRuntime(SQLException parent) {
|
protected SQLExceptionRuntime(SQLException parent) {
|
||||||
super(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) {
|
public static SQLExceptionRuntime wrap(SQLException e) {
|
||||||
return new SQLExceptionRuntime(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) {
|
public static SQLExceptionRuntime wrapAndClose(AutoCloseable closeable,SQLException e) {
|
||||||
return close(closeable,wrap(e));
|
return close(closeable,wrap(e));
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,8 +32,18 @@ import net.forwardfire.tpquery.TPQFactory;
|
||||||
import net.forwardfire.tpquery.store.executor.AbstractTPQExecutor;
|
import net.forwardfire.tpquery.store.executor.AbstractTPQExecutor;
|
||||||
import net.forwardfire.tpquery.store.executor.TPQExecutorContext;
|
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> {
|
public class TPQExecutorJdbc extends AbstractTPQExecutor<Connection,PreparedStatement> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created the jdbc query executor.
|
||||||
|
* @param context The executor context.
|
||||||
|
*/
|
||||||
public TPQExecutorJdbc(TPQExecutorContext context) {
|
public TPQExecutorJdbc(TPQExecutorContext context) {
|
||||||
super(context);
|
super(context);
|
||||||
registrateStatementCreator(TPQFactory.StatementLanguage.SQL, (connection,statementText) -> {
|
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) {
|
public PreparedStatement execute(Connection connection,String queryName) {
|
||||||
return execute(connection, queryName, null);
|
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) {
|
public PreparedStatement execute(Connection connection,String queryName,Map<String,Object> parameters) {
|
||||||
return execute(createPreparedStatement(connection, queryName, parameters));
|
return execute(createPreparedStatement(connection, queryName, parameters));
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,21 +32,47 @@ import net.forwardfire.tpquery.TPQFactory;
|
||||||
import net.forwardfire.tpquery.store.executor.AbstractTPQExecutor;
|
import net.forwardfire.tpquery.store.executor.AbstractTPQExecutor;
|
||||||
import net.forwardfire.tpquery.store.executor.TPQExecutorContext;
|
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> {
|
public class TPQExecutorJpa extends AbstractTPQExecutor<EntityManager,Query> {
|
||||||
|
|
||||||
private static final String HINT_TIMEOUT = "javax.persistence.query.timeout";
|
private static final String HINT_TIMEOUT = "javax.persistence.query.timeout";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created the jpa query executor.
|
||||||
|
* @param context The executor context.
|
||||||
|
*/
|
||||||
public TPQExecutorJpa(TPQExecutorContext context) {
|
public TPQExecutorJpa(TPQExecutorContext context) {
|
||||||
super(context);
|
super(context);
|
||||||
registrateStatementCreator(TPQFactory.StatementLanguage.HQL, (em,stmt) -> em.createQuery(stmt));
|
registrateStatementCreator(TPQFactory.StatementLanguage.HQL, (em,stmt) -> em.createQuery(stmt));
|
||||||
registrateStatementCreator(TPQFactory.StatementLanguage.SQL, (em,stmt) -> em.createNativeQuery(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")
|
@SuppressWarnings("unchecked")
|
||||||
public <E> List<E> selectList(EntityManager entityManager,String queryName,Map<String,Object> parameters) {
|
public <E> List<E> selectList(EntityManager entityManager,String queryName,Map<String,Object> parameters) {
|
||||||
return createPreparedStatement(entityManager, queryName, parameters).getResultList();
|
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")
|
@SuppressWarnings("unchecked")
|
||||||
public <E> E selectObject(EntityManager entityManager,String queryName,Map<String,Object> parameters) {
|
public <E> E selectObject(EntityManager entityManager,String queryName,Map<String,Object> parameters) {
|
||||||
return (E)createPreparedStatement(entityManager, queryName, parameters).getSingleResult();
|
return (E)createPreparedStatement(entityManager, queryName, parameters).getSingleResult();
|
||||||
|
|
|
@ -157,7 +157,9 @@ public final class TPQFactory {
|
||||||
|
|
||||||
public static final class StatementLanguage implements TPQConfigInitializer {
|
public static final class StatementLanguage implements TPQConfigInitializer {
|
||||||
|
|
||||||
|
/** The name of the sql language. */
|
||||||
public static final String SQL = "SQL";
|
public static final String SQL = "SQL";
|
||||||
|
/** The name of the hql language. */
|
||||||
public static final String HQL = "HQL";
|
public static final String HQL = "HQL";
|
||||||
|
|
||||||
private StatementLanguage() {
|
private StatementLanguage() {
|
||||||
|
@ -171,9 +173,13 @@ public final class TPQFactory {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final class StatementParameter implements TPQConfigInitializer {
|
public static final class StatementParameter implements TPQConfigInitializer {
|
||||||
|
/** The value type parameter. */
|
||||||
public static final String VALUE = "VALUE";
|
public static final String VALUE = "VALUE";
|
||||||
|
/** The list type parameter. */
|
||||||
public static final String LIST = "LIST";
|
public static final String LIST = "LIST";
|
||||||
|
/** The raw type parameter. */
|
||||||
public static final String RAW = "RAW";
|
public static final String RAW = "RAW";
|
||||||
|
/** The raw_null type parameter. */
|
||||||
public static final String RAW_NULL = "RAW_NULL";
|
public static final String RAW_NULL = "RAW_NULL";
|
||||||
|
|
||||||
private StatementParameter() {
|
private StatementParameter() {
|
||||||
|
@ -190,29 +196,51 @@ public final class TPQFactory {
|
||||||
|
|
||||||
public static final class ParameterValueType {
|
public static final class ParameterValueType {
|
||||||
// java type
|
// java type
|
||||||
|
/** The class name of an Object. */
|
||||||
public static final String JAVA_OBJECT = Object.class.getName();
|
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();
|
public static final String BIGDECIMAL = java.math.BigDecimal.class.getName();
|
||||||
|
/** The class name of an Boolean. */
|
||||||
public static final String BOOLEAN = Boolean.class.getName();
|
public static final String BOOLEAN = Boolean.class.getName();
|
||||||
|
/** The class name of an Integer. */
|
||||||
public static final String INTEGER = Integer.class.getName();
|
public static final String INTEGER = Integer.class.getName();
|
||||||
|
/** The class name of an String. */
|
||||||
public static final String STRING = String.class.getName();
|
public static final String STRING = String.class.getName();
|
||||||
|
/** The class name of an Double. */
|
||||||
public static final String DOUBLE = Double.class.getName();
|
public static final String DOUBLE = Double.class.getName();
|
||||||
|
/** The class name of an Long. */
|
||||||
public static final String LONG = Long.class.getName();
|
public static final String LONG = Long.class.getName();
|
||||||
|
/** The class name of an Float. */
|
||||||
public static final String FLOAT = Float.class.getName();
|
public static final String FLOAT = Float.class.getName();
|
||||||
|
/** The class name of an Short. */
|
||||||
public static final String SHORT = Short.class.getName();
|
public static final String SHORT = Short.class.getName();
|
||||||
|
/** The class name of an Url. */
|
||||||
public static final String URL = java.net.URL.class.getName();
|
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();
|
public static final String BYTE_DATA = byte[].class.getName();
|
||||||
|
|
||||||
// jdbc types
|
// jdbc types
|
||||||
|
/** The class name of an java.sql.Array. */
|
||||||
public static final String SQL_ARRAY = java.sql.Array.class.getName();
|
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();
|
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();
|
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();
|
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();
|
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();
|
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();
|
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();
|
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();
|
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();
|
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();
|
public static final String SQL_TIMESTAMP = java.sql.Timestamp.class.getName();
|
||||||
|
|
||||||
protected ParameterValueType() {
|
protected ParameterValueType() {
|
||||||
|
@ -349,14 +377,23 @@ public final class TPQFactory {
|
||||||
private static final String FUNCTION_ARGS_END = ")";
|
private static final String FUNCTION_ARGS_END = ")";
|
||||||
private static final String FUNCTION_ARGS_EMPTY = FUNCTION_ARGS_BEGIN+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";
|
public static final String CREATE_OBJECT = "createObject";
|
||||||
|
/** Creates an arraylist. */
|
||||||
public static final String CREATE_ARRAY_LIST = "createArrayList";
|
public static final String CREATE_ARRAY_LIST = "createArrayList";
|
||||||
|
/** Creates an hashmap object. */
|
||||||
public static final String CREATE_HASH_MAP = "createHashMap";
|
public static final String CREATE_HASH_MAP = "createHashMap";
|
||||||
|
/** Creates an arraylist of objects. */
|
||||||
public static final String CREATE_OBJECT_LIST = "createObjectList";
|
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";
|
public static final String CREATE_VALUE_HOLDER = "createValueHolder";
|
||||||
|
/** Creates an epoch long value. */
|
||||||
public static final String EPOCH = "epoch";
|
public static final String EPOCH = "epoch";
|
||||||
|
/** Creates an date object. */
|
||||||
public static final String DATE = "date";
|
public static final String DATE = "date";
|
||||||
|
/** Creates an time object. */
|
||||||
public static final String TIME = "time";
|
public static final String TIME = "time";
|
||||||
|
/** Creates an timestamp object. */
|
||||||
public static final String TIMESTAMP = "timestamp";
|
public static final String TIMESTAMP = "timestamp";
|
||||||
|
|
||||||
private ParameterValueFunction() {
|
private ParameterValueFunction() {
|
||||||
|
|
|
@ -28,11 +28,19 @@ import net.forwardfire.tpquery.model.TPQueryHint;
|
||||||
/**
|
/**
|
||||||
* Abstract query node builder for the shared properties of the AbstractTPQueryNode.
|
* 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
|
* @author Willem Cazander
|
||||||
* @version 1.0 May 29, 2015
|
* @version 1.0 May 29, 2015
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractTPQueryNodeBuilder<P,T extends AbstractTPQueryNode,B> extends AbstractTPQBuilder<P,T,B> {
|
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) {
|
public AbstractTPQueryNodeBuilder(P parent, T build) {
|
||||||
super(parent, build);
|
super(parent, build);
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,6 +39,7 @@ import net.forwardfire.tpquery.statement.TPQStatementMarshaller;
|
||||||
/**
|
/**
|
||||||
* The root config builder.
|
* The root config builder.
|
||||||
*
|
*
|
||||||
|
* @param <T> The object to build.
|
||||||
* @author Willem Cazander
|
* @author Willem Cazander
|
||||||
* @version 1.0 May 22, 2015
|
* @version 1.0 May 22, 2015
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -33,6 +33,8 @@ import net.forwardfire.tpquery.statement.TPQStatementPartText;
|
||||||
/**
|
/**
|
||||||
* Builds queries.
|
* Builds queries.
|
||||||
*
|
*
|
||||||
|
* @param <P> The parent builder.
|
||||||
|
* @param <T> The object to build.
|
||||||
* @author Willem Cazander
|
* @author Willem Cazander
|
||||||
* @version 1.0 May 29, 2015
|
* @version 1.0 May 29, 2015
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -29,6 +29,8 @@ import net.forwardfire.tpquery.model.TPQueryParameter;
|
||||||
/**
|
/**
|
||||||
* Builds query parameters.
|
* Builds query parameters.
|
||||||
*
|
*
|
||||||
|
* @param <P> The parent builder.
|
||||||
|
* @param <T> The object to build.
|
||||||
* @author Willem Cazander
|
* @author Willem Cazander
|
||||||
* @version 1.0 May 29, 2015
|
* @version 1.0 May 29, 2015
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -28,6 +28,7 @@ import net.forwardfire.tpquery.model.TPQuerySet;
|
||||||
/**
|
/**
|
||||||
* Builds query set.
|
* Builds query set.
|
||||||
*
|
*
|
||||||
|
* @param <T> The object to build.
|
||||||
* @author Willem Cazander
|
* @author Willem Cazander
|
||||||
* @version 1.0 May 29, 2015
|
* @version 1.0 May 29, 2015
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -32,6 +32,7 @@ import net.forwardfire.tpquery.model.TPQuerySet;
|
||||||
/**
|
/**
|
||||||
* Builds query set as tree.
|
* Builds query set as tree.
|
||||||
*
|
*
|
||||||
|
* @param <T> The object to build.
|
||||||
* @author Willem Cazander
|
* @author Willem Cazander
|
||||||
* @version 1.0 May 29, 2015
|
* @version 1.0 May 29, 2015
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -36,6 +36,10 @@ public final class TPQConfigValidatorCheckNamePattern implements TPQConfigValida
|
||||||
|
|
||||||
private final String pattern;
|
private final String pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an name pattern checker.
|
||||||
|
* @param pattern The pattern to check the name against.
|
||||||
|
*/
|
||||||
public TPQConfigValidatorCheckNamePattern(String pattern) {
|
public TPQConfigValidatorCheckNamePattern(String pattern) {
|
||||||
Validate.matchesPattern("", pattern);
|
Validate.matchesPattern("", pattern);
|
||||||
this.pattern=pattern;
|
this.pattern=pattern;
|
||||||
|
|
|
@ -30,6 +30,10 @@ package net.forwardfire.tpquery.config.validate;
|
||||||
*/
|
*/
|
||||||
public final class TPQConfigValidatorCheckParameterNamePattern extends AbstractTPQConfigValidatorParameterPattern {
|
public final class TPQConfigValidatorCheckParameterNamePattern extends AbstractTPQConfigValidatorParameterPattern {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an name pattern checker.
|
||||||
|
* @param pattern The pattern to check the name against.
|
||||||
|
*/
|
||||||
public TPQConfigValidatorCheckParameterNamePattern(String pattern) {
|
public TPQConfigValidatorCheckParameterNamePattern(String pattern) {
|
||||||
super(pattern,p -> p.getName());
|
super(pattern,p -> p.getName());
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,10 @@ package net.forwardfire.tpquery.config.validate;
|
||||||
*/
|
*/
|
||||||
public final class TPQConfigValidatorCheckParameterTypePattern extends AbstractTPQConfigValidatorParameterPattern {
|
public final class TPQConfigValidatorCheckParameterTypePattern extends AbstractTPQConfigValidatorParameterPattern {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an type pattern checker.
|
||||||
|
* @param pattern The pattern to check the type against.
|
||||||
|
*/
|
||||||
public TPQConfigValidatorCheckParameterTypePattern(String pattern) {
|
public TPQConfigValidatorCheckParameterTypePattern(String pattern) {
|
||||||
super(pattern,p -> p.getType());
|
super(pattern,p -> p.getType());
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,10 @@ package net.forwardfire.tpquery.config.validate;
|
||||||
*/
|
*/
|
||||||
public final class TPQConfigValidatorCheckParameterValueTypePattern extends AbstractTPQConfigValidatorParameterPattern {
|
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) {
|
public TPQConfigValidatorCheckParameterValueTypePattern(String pattern) {
|
||||||
super(pattern,p -> p.getValueType());
|
super(pattern,p -> p.getValueType());
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,6 +47,9 @@ public abstract class AbstractTPQueryNode {
|
||||||
private Integer timeout = null;
|
private Integer timeout = null;
|
||||||
private final List<TPQueryHint> queryHints;
|
private final List<TPQueryHint> queryHints;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an query node.
|
||||||
|
*/
|
||||||
public AbstractTPQueryNode() {
|
public AbstractTPQueryNode() {
|
||||||
queryHints = new ArrayList<>();
|
queryHints = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,11 +44,18 @@ public final class TPQuery extends AbstractTPQueryNode {
|
||||||
private List<TPQStatement> queryParts;
|
private List<TPQStatement> queryParts;
|
||||||
private final List<TPQueryParameter> queryParameters;
|
private final List<TPQueryParameter> queryParameters;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a query.
|
||||||
|
*/
|
||||||
public TPQuery() {
|
public TPQuery() {
|
||||||
queryParts = new ArrayList<>();
|
queryParts = new ArrayList<>();
|
||||||
queryParameters = new ArrayList<>();
|
queryParameters = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a query.
|
||||||
|
* @param name The name of the query.
|
||||||
|
*/
|
||||||
public TPQuery(String name) {
|
public TPQuery(String name) {
|
||||||
this();
|
this();
|
||||||
setName(name);
|
setName(name);
|
||||||
|
@ -62,12 +69,17 @@ public final class TPQuery extends AbstractTPQueryNode {
|
||||||
public List<TPQStatement> getQueryParts() {
|
public List<TPQStatement> getQueryParts() {
|
||||||
return queryParts;
|
return queryParts;
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
public void setQueryParts(List<TPQStatement> parts) {
|
public void setQueryParts(List<TPQStatement> parts) {
|
||||||
Objects.requireNonNull(parts,"Can't set null list");
|
Objects.requireNonNull(parts,"Can't set null list");
|
||||||
queryParts = parts;
|
queryParts = parts;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an query part.
|
||||||
|
* @param queryPart The part to add.
|
||||||
|
*/
|
||||||
public void addQueryPart(TPQStatement queryPart) {
|
public void addQueryPart(TPQStatement queryPart) {
|
||||||
Objects.requireNonNull(queryPart,"Can't add null");
|
Objects.requireNonNull(queryPart,"Can't add null");
|
||||||
queryParts.add(queryPart);
|
queryParts.add(queryPart);
|
||||||
|
@ -81,6 +93,10 @@ public final class TPQuery extends AbstractTPQueryNode {
|
||||||
return queryParameters;
|
return queryParameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an queryParameter.
|
||||||
|
* @param queryParameter The parameter to add.
|
||||||
|
*/
|
||||||
public void addQueryParameter(TPQueryParameter queryParameter) {
|
public void addQueryParameter(TPQueryParameter queryParameter) {
|
||||||
Objects.requireNonNull(queryParameter,"Can't add null");
|
Objects.requireNonNull(queryParameter,"Can't add null");
|
||||||
Objects.requireNonNull(queryParameter.getName(),"TQueryParameter.getName() is null.");
|
Objects.requireNonNull(queryParameter.getName(),"TQueryParameter.getName() is null.");
|
||||||
|
|
|
@ -37,16 +37,20 @@ public final class TPQueryHint {
|
||||||
private String name = null;
|
private String name = null;
|
||||||
private String value = null;
|
private String value = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a query hint.
|
||||||
|
*/
|
||||||
public TPQueryHint() {
|
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();
|
this();
|
||||||
setName(name);
|
setName(name);
|
||||||
}
|
|
||||||
|
|
||||||
public TPQueryHint(String name,String value) {
|
|
||||||
this(name);
|
|
||||||
setValue(value);
|
setValue(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,9 +40,17 @@ public final class TPQueryParameter {
|
||||||
private String valueType = null;
|
private String valueType = null;
|
||||||
private Boolean nullable = null;
|
private Boolean nullable = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a query parameter.
|
||||||
|
*/
|
||||||
public TPQueryParameter() {
|
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) {
|
public TPQueryParameter(String name,String valueType) {
|
||||||
setName(name);
|
setName(name);
|
||||||
setValueType(valueType);
|
setValueType(valueType);
|
||||||
|
|
|
@ -42,11 +42,18 @@ public class TPQuerySet extends AbstractTPQueryNode {
|
||||||
private final List<TPQuerySet> querySets;
|
private final List<TPQuerySet> querySets;
|
||||||
private final List<TPQuery> queries;
|
private final List<TPQuery> queries;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a query set.
|
||||||
|
*/
|
||||||
public TPQuerySet() {
|
public TPQuerySet() {
|
||||||
queries = new ArrayList<>();
|
queries = new ArrayList<>();
|
||||||
querySets = new ArrayList<>();
|
querySets = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a query set.
|
||||||
|
* @param name The name of the query set.
|
||||||
|
*/
|
||||||
public TPQuerySet(String name) {
|
public TPQuerySet(String name) {
|
||||||
this();
|
this();
|
||||||
setName(name);
|
setName(name);
|
||||||
|
|
|
@ -41,15 +41,29 @@ public class TPQuerySetXMLMarshaller extends AbstractXMLMarshaller {
|
||||||
|
|
||||||
private final JAXBContext querySetContext;
|
private final JAXBContext querySetContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a query set xml marshaller.
|
||||||
|
* @throws JAXBException When context could not be build.
|
||||||
|
*/
|
||||||
public TPQuerySetXMLMarshaller() throws JAXBException {
|
public TPQuerySetXMLMarshaller() throws JAXBException {
|
||||||
this(JAXBContext.newInstance(TPQuerySet.class));
|
this(JAXBContext.newInstance(TPQuerySet.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a query set xml marshaller.
|
||||||
|
* @param querySetContext The jaxb query set context.
|
||||||
|
*/
|
||||||
public TPQuerySetXMLMarshaller(JAXBContext querySetContext) {
|
public TPQuerySetXMLMarshaller(JAXBContext querySetContext) {
|
||||||
Objects.requireNonNull(querySetContext,"querySetContext is null.");
|
Objects.requireNonNull(querySetContext,"querySetContext is null.");
|
||||||
this.querySetContext = querySetContext;
|
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 {
|
public void marshal(TPQuerySet querySet, OutputStream output) throws JAXBException {
|
||||||
Objects.requireNonNull(querySet,"querySet is null.");
|
Objects.requireNonNull(querySet,"querySet is null.");
|
||||||
Objects.requireNonNull(output,"OutputStream is null.");
|
Objects.requireNonNull(output,"OutputStream is null.");
|
||||||
|
@ -60,6 +74,12 @@ public class TPQuerySetXMLMarshaller extends AbstractXMLMarshaller {
|
||||||
marshaller.marshal(querySet, output);
|
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 {
|
public TPQuerySet unmarshal(InputStream input) throws JAXBException {
|
||||||
Objects.requireNonNull(input,"InputStream is null.");
|
Objects.requireNonNull(input,"InputStream is null.");
|
||||||
|
|
||||||
|
|
|
@ -38,6 +38,10 @@ public abstract class AbstractTPQStatement implements TPQStatement {
|
||||||
private TPQStatementContext context;
|
private TPQStatementContext context;
|
||||||
private TPQuery query;
|
private TPQuery query;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an statement.
|
||||||
|
* @param statementPart The text value of the statement part.
|
||||||
|
*/
|
||||||
public AbstractTPQStatement(String statementPart) {
|
public AbstractTPQStatement(String statementPart) {
|
||||||
Validate.notNull(statementPart,"statementPart is null");
|
Validate.notNull(statementPart,"statementPart is null");
|
||||||
this.statementPart=statementPart;
|
this.statementPart=statementPart;
|
||||||
|
@ -50,6 +54,9 @@ public abstract class AbstractTPQStatement implements TPQStatement {
|
||||||
prepareInit();
|
prepareInit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* init this statement.
|
||||||
|
*/
|
||||||
protected void prepareInit() {
|
protected void prepareInit() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,18 +44,30 @@ public abstract class AbstractTPQStatementWriter implements TPQStatementWriter {
|
||||||
protected static final String PARAMETER_NEXT = ",";
|
protected static final String PARAMETER_NEXT = ",";
|
||||||
private static final int OUTPUT_START_SIZE = 384;
|
private static final int OUTPUT_START_SIZE = 384;
|
||||||
|
|
||||||
protected boolean flagValueBased = false;
|
private final StringBuilder outputBuffer;
|
||||||
protected final StringBuilder output;
|
private final List<TPQueryStoreStatementMapper> queryParameterMapping;
|
||||||
protected final List<TPQueryStoreStatementMapper> queryParameterMapping;
|
private final Map<String,Object> queryParameterData;
|
||||||
protected final Map<String,Object> queryParameterData;
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an writer.
|
||||||
|
* @param queryParameterData The parameter value data.
|
||||||
|
*/
|
||||||
public AbstractTPQStatementWriter(Map<String,Object> queryParameterData) {
|
public AbstractTPQStatementWriter(Map<String,Object> queryParameterData) {
|
||||||
Validate.notNull(queryParameterData);
|
Validate.notNull(queryParameterData);
|
||||||
this.queryParameterData=queryParameterData;
|
this.queryParameterData=queryParameterData;
|
||||||
this.output = new StringBuilder(OUTPUT_START_SIZE);
|
this.outputBuffer = new StringBuilder(OUTPUT_START_SIZE);
|
||||||
this.queryParameterMapping = new ArrayList<>();
|
this.queryParameterMapping = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Returns the output buffer.
|
||||||
|
*/
|
||||||
|
protected StringBuilder getOutputBuffer() {
|
||||||
|
return outputBuffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------ TPQStatementWriter methods
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeQuery(TPQuery query) {
|
public void writeQuery(TPQuery query) {
|
||||||
Validate.notNull(query,"Can' write null query ");
|
Validate.notNull(query,"Can' write null query ");
|
||||||
|
@ -75,19 +87,19 @@ public abstract class AbstractTPQStatementWriter implements TPQStatementWriter {
|
||||||
@Override
|
@Override
|
||||||
public void appendQueryText(String text) {
|
public void appendQueryText(String text) {
|
||||||
Validate.notNull(text, "Can't add null text.");
|
Validate.notNull(text, "Can't add null text.");
|
||||||
output.append(text);
|
outputBuffer.append(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void appendQueryParameter(TPQueryStoreStatementMapper mapper) {
|
public void appendQueryParameter(TPQueryStoreStatementMapper mapper) {
|
||||||
Validate.notNull(mapper);
|
Validate.notNull(mapper);
|
||||||
queryParameterMapping.add(mapper);
|
queryParameterMapping.add(mapper);
|
||||||
output.append(PARAMETER_PREPARED);
|
outputBuffer.append(PARAMETER_PREPARED);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void appendQueryParameterSeperator() {
|
public void appendQueryParameterSeperator() {
|
||||||
output.append(PARAMETER_NEXT);
|
outputBuffer.append(PARAMETER_NEXT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -97,6 +109,6 @@ public abstract class AbstractTPQStatementWriter implements TPQStatementWriter {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getQueryText() {
|
public String getQueryText() {
|
||||||
return output.toString();
|
return outputBuffer.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,6 +40,11 @@ public class TPQStatementMarshaller {
|
||||||
protected static final String INCLUDE_PREFIX = "inc:";
|
protected static final String INCLUDE_PREFIX = "inc:";
|
||||||
private static final int DEFAULT_PRINT_SIZE = 384;
|
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) {
|
public String marshal(List<TPQStatement> parts) {
|
||||||
Objects.requireNonNull(parts,"Can't print null list.");
|
Objects.requireNonNull(parts,"Can't print null list.");
|
||||||
StringBuilder buf = new StringBuilder(DEFAULT_PRINT_SIZE);
|
StringBuilder buf = new StringBuilder(DEFAULT_PRINT_SIZE);
|
||||||
|
@ -60,10 +65,15 @@ public class TPQStatementMarshaller {
|
||||||
return buf.toString();
|
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<>();
|
List<TPQStatement> result = new ArrayList<>();
|
||||||
parseLoop(result,statementTemplateText);
|
parseLoop(result,statementText);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,11 @@ public interface TPQStatementWriter {
|
||||||
*/
|
*/
|
||||||
void writeQuery(TPQuery query);
|
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);
|
Object getParameterValue(TPQueryParameter parameter);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -56,7 +56,7 @@ public class TPQStatementLanguageHql extends AbstractTPQStatementLanguage {
|
||||||
@Override
|
@Override
|
||||||
public void appendQueryParameter(TPQueryStoreStatementMapper valueMapper) {
|
public void appendQueryParameter(TPQueryStoreStatementMapper valueMapper) {
|
||||||
super.appendQueryParameter(valueMapper);
|
super.appendQueryParameter(valueMapper);
|
||||||
output.append(this.queryParameterMapping.size());
|
getOutputBuffer().append(getQueryParameterMapping().size());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
* @author Willem Cazander
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/**
|
/**
|
||||||
* Statement parameter types: VALUE,LIST,RAW
|
* Statement parameter types: VALUE,LIST,RAW.
|
||||||
*
|
*
|
||||||
* @author Willem Cazander
|
* @author Willem Cazander
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -47,42 +47,69 @@ public class TPQueryStoreStatement implements Serializable {
|
||||||
private String statement;
|
private String statement;
|
||||||
private String language;
|
private String language;
|
||||||
private Integer timeout;
|
private Integer timeout;
|
||||||
private Map<String,String> properties;
|
private Map<String,String> queryHints;
|
||||||
private List<TPQueryStoreStatementMapper> valueMapping;
|
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(name,"name is null");
|
||||||
Objects.requireNonNull(statement,"statement is null");
|
Objects.requireNonNull(statement,"statement is null");
|
||||||
Objects.requireNonNull(language,"language is null");
|
Objects.requireNonNull(language,"language is null");
|
||||||
Objects.requireNonNull(properties,"properties is null");
|
Objects.requireNonNull(queryHints,"properties is null");
|
||||||
this.name=name;
|
this.name=name;
|
||||||
this.statement=statement;
|
this.statement=statement;
|
||||||
this.timeout=timeout; // null is no timeout
|
this.timeout=timeout; // null is no timeout
|
||||||
this.language=language;
|
this.language=language;
|
||||||
this.properties=Collections.unmodifiableMap(properties);
|
this.queryHints=Collections.unmodifiableMap(queryHints);
|
||||||
this.valueMapping=Collections.unmodifiableList(valueMapping);
|
this.valueMapping=Collections.unmodifiableList(valueMapping);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The name of the statement.
|
||||||
|
*/
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The statement text.
|
||||||
|
*/
|
||||||
public String getStatement() {
|
public String getStatement() {
|
||||||
return statement;
|
return statement;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The statement langauge.
|
||||||
|
*/
|
||||||
public String getLanguage() {
|
public String getLanguage() {
|
||||||
return language;
|
return language;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The statement timeout.
|
||||||
|
*/
|
||||||
public Integer getTimeout() {
|
public Integer getTimeout() {
|
||||||
return timeout;
|
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() {
|
public List<TPQueryStoreStatementMapper> getValueMapping() {
|
||||||
return valueMapping;
|
return valueMapping;
|
||||||
}
|
}
|
||||||
|
@ -94,7 +121,7 @@ public class TPQueryStoreStatement implements Serializable {
|
||||||
out.writeUTF(statement);
|
out.writeUTF(statement);
|
||||||
out.writeUTF(language);
|
out.writeUTF(language);
|
||||||
out.writeObject(timeout);
|
out.writeObject(timeout);
|
||||||
out.writeObject(new HashMap<>(properties));
|
out.writeObject(new HashMap<>(queryHints));
|
||||||
out.writeObject(new ArrayList<>(valueMapping));
|
out.writeObject(new ArrayList<>(valueMapping));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,7 +131,7 @@ public class TPQueryStoreStatement implements Serializable {
|
||||||
statement = in.readUTF();
|
statement = in.readUTF();
|
||||||
language = in.readUTF();
|
language = in.readUTF();
|
||||||
timeout = (Integer)in.readObject();
|
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());
|
valueMapping = Collections.unmodifiableList((List<TPQueryStoreStatementMapper>) in.readObject());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,12 +28,23 @@ import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.function.BiFunction;
|
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> {
|
public abstract class AbstractTPQExecutor<C,S> {
|
||||||
|
|
||||||
private static final int PARAMETER_INDEX_START = 1;
|
private static final int PARAMETER_INDEX_START = 1;
|
||||||
private final TPQExecutorContext context;
|
private final TPQExecutorContext context;
|
||||||
private final Map<String,BiFunction<C,String,S>> backendTypes;
|
private final Map<String,BiFunction<C,String,S>> backendTypes;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates the executor with a context.
|
||||||
|
* @param context The executor context.
|
||||||
|
*/
|
||||||
public AbstractTPQExecutor(TPQExecutorContext context) {
|
public AbstractTPQExecutor(TPQExecutorContext context) {
|
||||||
Objects.requireNonNull(context,"Can't execute with null context.");
|
Objects.requireNonNull(context,"Can't execute with null context.");
|
||||||
this.context = context;
|
this.context = context;
|
||||||
|
@ -46,18 +57,41 @@ public abstract class AbstractTPQExecutor<C,S> {
|
||||||
backendTypes.put(language, statementCreator);
|
backendTypes.put(language, statementCreator);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The executor context.
|
||||||
|
*/
|
||||||
protected final TPQExecutorContext getContext() {
|
protected final TPQExecutorContext getContext() {
|
||||||
return context;
|
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) {
|
public S createPreparedStatement(C connection,String queryName,Map<String,Object> parameters) {
|
||||||
return prepare(connection,getContext().prepareQuery(queryName,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) {
|
public int executeUpdate(C connection,String queryName) {
|
||||||
return executeUpdate(connection, queryName, null);
|
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) {
|
public int executeUpdate(C connection,String queryName,Map<String,Object> parameters) {
|
||||||
return executeUpdate(createPreparedStatement(connection, queryName, parameters));
|
return executeUpdate(createPreparedStatement(connection, queryName, parameters));
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,22 +26,40 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Statement for the executor.
|
||||||
*
|
*
|
||||||
* @author Willem Cazander
|
* @author Willem Cazander
|
||||||
* @version 1.0 Jan 14, 2015
|
* @version 1.0 Jan 14, 2015
|
||||||
*/
|
*/
|
||||||
public interface TPQExecutorStatement {
|
public interface TPQExecutorStatement {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The name of the statement.
|
||||||
|
*/
|
||||||
String getName();
|
String getName();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The statement text.
|
||||||
|
*/
|
||||||
String getStatement();
|
String getStatement();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The parameters in ordered state.
|
||||||
|
*/
|
||||||
List<Object> getParameters();
|
List<Object> getParameters();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The timeout of the request.
|
||||||
|
*/
|
||||||
Integer getTimeout();
|
Integer getTimeout();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The language of the statement text.
|
||||||
|
*/
|
||||||
String getLanguage();
|
String getLanguage();
|
||||||
|
|
||||||
Map<String,String> getProperties();
|
/**
|
||||||
|
* @return The query hints.
|
||||||
|
*/
|
||||||
|
Map<String,String> getQueryHints();
|
||||||
}
|
}
|
||||||
|
|
|
@ -174,7 +174,7 @@ public final class TPQStoreManagerConfig extends AbstractTPQConfig {
|
||||||
LOG.debug("Created store entry for query: {} systemId: {}",entry.getQuery().getName(),entry.getSystemId());
|
LOG.debug("Created store entry for query: {} systemId: {}",entry.getQuery().getName(),entry.getSystemId());
|
||||||
|
|
||||||
q.getQueryHints().forEach(hint -> {
|
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());
|
LOG.trace("Mapped query hint name: {} value {}",hint.getName(),hint.getValue());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -52,7 +52,7 @@ public final class TPQStoreManagerEntry {
|
||||||
private final String systemId;
|
private final String systemId;
|
||||||
private boolean valueMappedWriter = true;
|
private boolean valueMappedWriter = true;
|
||||||
private boolean validateParameters = 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,CompiledScript> parameterDefaultCompiledScripts;
|
||||||
private final Map<String,TPQConfigParameterValueHolder> parameterDefaultValues;
|
private final Map<String,TPQConfigParameterValueHolder> parameterDefaultValues;
|
||||||
private Map<String,TPQueryParameterMetaData> parameterMetaData;
|
private Map<String,TPQueryParameterMetaData> parameterMetaData;
|
||||||
|
@ -68,7 +68,7 @@ public final class TPQStoreManagerEntry {
|
||||||
Objects.requireNonNull(systemId);
|
Objects.requireNonNull(systemId);
|
||||||
this.query=query;
|
this.query=query;
|
||||||
this.systemId=systemId;
|
this.systemId=systemId;
|
||||||
this.namedProperties=new HashMap<>();
|
this.queryHints=new HashMap<>();
|
||||||
this.parameterDefaultCompiledScripts=new HashMap<>();
|
this.parameterDefaultCompiledScripts=new HashMap<>();
|
||||||
this.parameterDefaultValues=new HashMap<>();
|
this.parameterDefaultValues=new HashMap<>();
|
||||||
for (TPQueryHint hint:query.getQueryHints()) {
|
for (TPQueryHint hint:query.getQueryHints()) {
|
||||||
|
@ -123,8 +123,8 @@ public final class TPQStoreManagerEntry {
|
||||||
/**
|
/**
|
||||||
* @return the properties
|
* @return the properties
|
||||||
*/
|
*/
|
||||||
public Map<String, String> getNamedProperties() {
|
public Map<String, String> getQueryHints() {
|
||||||
return namedProperties;
|
return queryHints;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -43,6 +43,8 @@ public class TPQStoreManagerEntryParameterMetaData implements TPQueryParameterMe
|
||||||
* Create an info for the parameter.
|
* Create an info for the parameter.
|
||||||
* @param name The parameter name.
|
* @param name The parameter name.
|
||||||
* @param valueType The parameter type.
|
* @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) {
|
public TPQStoreManagerEntryParameterMetaData(String name,Class<?> valueType,boolean nullable,boolean required) {
|
||||||
Objects.requireNonNull(name);
|
Objects.requireNonNull(name);
|
||||||
|
|
|
@ -27,6 +27,7 @@ import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.script.CompiledScript;
|
import javax.script.CompiledScript;
|
||||||
|
@ -77,7 +78,8 @@ public class TPQStoreManagerExecutorContext implements TPQExecutorContext {
|
||||||
public TPQExecutorStatement prepareQuery(String queryName,Map<String,Object> para) {
|
public TPQExecutorStatement prepareQuery(String queryName,Map<String,Object> para) {
|
||||||
TPQStoreManagerEntry entry = config.getEntries().get(queryName);
|
TPQStoreManagerEntry entry = config.getEntries().get(queryName);
|
||||||
Validate.notNull(entry, "Could not find query: %s",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) {
|
private TPQExecutorStatement preparedQueryEntry(TPQStoreManagerEntry entry,Map<String,Object> para) {
|
||||||
|
@ -217,7 +219,7 @@ public class TPQStoreManagerExecutorContext implements TPQExecutorContext {
|
||||||
qw.getQueryText(),
|
qw.getQueryText(),
|
||||||
me.getQuery().getLanguage(),
|
me.getQuery().getLanguage(),
|
||||||
me.getQuery().getTimeout(),
|
me.getQuery().getTimeout(),
|
||||||
me.getNamedProperties(),
|
me.getQueryHints(),
|
||||||
qw.getQueryParameterMapping()
|
qw.getQueryParameterMapping()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,7 +79,7 @@ public final class TPQStoreManagerExecutorStatement implements TPQExecutorStatem
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String,String> getProperties() {
|
public Map<String,String> getQueryHints() {
|
||||||
return statement.getProperties();
|
return statement.getQueryHints();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,7 @@ public class TPQStoreManagerStatementCache implements TPQueryStoreStatementCache
|
||||||
/**
|
/**
|
||||||
* The default cache map initial capacity (MUST be a power of two)
|
* 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.
|
* The cache data.
|
||||||
|
|
|
@ -41,7 +41,6 @@ public @interface TPQueryName {
|
||||||
* Sets an non default query name.
|
* Sets an non default query name.
|
||||||
* It defaults to the method name.
|
* It defaults to the method name.
|
||||||
* note: result is formatted with the method name as 1st parameter.
|
* note: result is formatted with the method name as 1st parameter.
|
||||||
*
|
|
||||||
* @return The query name.
|
* @return The query name.
|
||||||
*/
|
*/
|
||||||
String value() default "%s";
|
String value() default "%s";
|
||||||
|
|
|
@ -38,6 +38,7 @@ import java.lang.annotation.Target;
|
||||||
public @interface TPQueryParameterName {
|
public @interface TPQueryParameterName {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* The query parameter name.
|
||||||
* @return The query parameter name.
|
* @return The query parameter name.
|
||||||
*/
|
*/
|
||||||
String value();
|
String value();
|
||||||
|
|
|
@ -41,8 +41,7 @@ public @interface TPQueryProxy {
|
||||||
* Sets an non default query prefix.
|
* Sets an non default query prefix.
|
||||||
* It defaults to an empty string.
|
* It defaults to an empty string.
|
||||||
* note: result is formatted with the interface simple class name as 1st parameter.
|
* note: result is formatted with the interface simple class name as 1st parameter.
|
||||||
*
|
* @return The query prefix.
|
||||||
* @return The prefix of the queries in the interface of the method.
|
|
||||||
*/
|
*/
|
||||||
String prefix() default "";
|
String prefix() default "";
|
||||||
}
|
}
|
||||||
|
|
|
@ -73,6 +73,7 @@ public class TPQueryProxyFactory {
|
||||||
/**
|
/**
|
||||||
* Creates an new proxy instance for the query interface class.
|
* 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.
|
* @param queryInterface The class to make a query proxy for.
|
||||||
* @return The wired proxy.
|
* @return The wired proxy.
|
||||||
*/
|
*/
|
||||||
|
@ -98,8 +99,12 @@ public class TPQueryProxyFactory {
|
||||||
TPQueryProxy queryProxy = Validate.notNull(queryInterface.getAnnotation(TPQueryProxy.class));
|
TPQueryProxy queryProxy = Validate.notNull(queryInterface.getAnnotation(TPQueryProxy.class));
|
||||||
String queryPrefix = String.format(queryProxy.prefix(),queryInterface.getSimpleName());
|
String queryPrefix = String.format(queryProxy.prefix(),queryInterface.getSimpleName());
|
||||||
for (Method method:queryInterface.getMethods()) {
|
for (Method method:queryInterface.getMethods()) {
|
||||||
|
String queryNamePart = method.getName();
|
||||||
TPQueryName query = method.getAnnotation(TPQueryName.class);
|
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());
|
BiFunction<String,Map<String,Object>,Object> handler = lookupInvokeHandler(method.getReturnType());
|
||||||
Function<Map<String,Object>,Object> invoker = parameters -> handler.apply(queryName, parameters);
|
Function<Map<String,Object>,Object> invoker = parameters -> handler.apply(queryName, parameters);
|
||||||
methodInvokers.put(method, invoker);
|
methodInvokers.put(method, invoker);
|
||||||
|
|
|
@ -113,15 +113,12 @@ public class TPQSpeedTest {
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
// warmup
|
// warmup
|
||||||
Map<String,Object> para1 = new HashMap<>(); // FIXME: ??? defaults are adding in this maps
|
Map<String,Object> para = new HashMap<>();
|
||||||
Map<String,Object> para2 = new HashMap<>();
|
|
||||||
Map<String,Object> para3 = new HashMap<>();
|
|
||||||
Map<String,Object> para4 = new HashMap<>();
|
|
||||||
for (int i=0;i<warmupSize;i++) {
|
for (int i=0;i<warmupSize;i++) {
|
||||||
queryManager.getQueryExecutorContext().prepareQuery("junit.speed-prepared", para1);
|
queryManager.getQueryExecutorContext().prepareQuery("junit.speed-prepared", para);
|
||||||
queryManager.getQueryExecutorContext().prepareQuery("junit.speed-raw", para2);
|
queryManager.getQueryExecutorContext().prepareQuery("junit.speed-raw", para);
|
||||||
queryManager.getQueryExecutorContext().prepareQuery("junit.speed-function-dynamic", para3);
|
queryManager.getQueryExecutorContext().prepareQuery("junit.speed-function-dynamic", para);
|
||||||
queryManager.getQueryExecutorContext().prepareQuery("junit.speed-function-holded", para4);
|
queryManager.getQueryExecutorContext().prepareQuery("junit.speed-function-holded", para);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -158,7 +158,7 @@ public class TPQueryStoreConfigTest {
|
||||||
assertNotNull(q.getTimeout());
|
assertNotNull(q.getTimeout());
|
||||||
assertNotNull(q.getStatement());
|
assertNotNull(q.getStatement());
|
||||||
assertNotNull(q.getParameters());
|
assertNotNull(q.getParameters());
|
||||||
assertNotNull(q.getProperties());
|
assertNotNull(q.getQueryHints());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -234,13 +234,13 @@ public class TPQueryStoreConfigTest {
|
||||||
.build();
|
.build();
|
||||||
assertNotNull(store);
|
assertNotNull(store);
|
||||||
TPQExecutorStatement q = store.getQueryExecutorContext().prepareQuery("junit.test", null);
|
TPQExecutorStatement q = store.getQueryExecutorContext().prepareQuery("junit.test", null);
|
||||||
assertNotNull(q.getProperties().get("p0"));
|
assertNotNull(q.getQueryHints().get("p0"));
|
||||||
assertEquals("v0",q.getProperties().get("p0"));
|
assertEquals("v0",q.getQueryHints().get("p0"));
|
||||||
assertEquals("v1",q.getProperties().get("p1"));
|
assertEquals("v1",q.getQueryHints().get("p1"));
|
||||||
|
|
||||||
TPQExecutorStatement qvv = store.getQueryExecutorContext().prepareQuery("junit.test-vv", null);
|
TPQExecutorStatement qvv = store.getQueryExecutorContext().prepareQuery("junit.test-vv", null);
|
||||||
assertNotNull(qvv.getProperties().get("p0"));
|
assertNotNull(qvv.getQueryHints().get("p0"));
|
||||||
assertEquals("vv",qvv.getProperties().get("p0"));
|
assertEquals("vv",qvv.getQueryHints().get("p0"));
|
||||||
assertEquals("v1",qvv.getProperties().get("p1"));
|
assertEquals("v1",qvv.getQueryHints().get("p1"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,7 +60,7 @@ public class TQueryExecutorTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, String> getProperties() {
|
public Map<String, String> getQueryHints() {
|
||||||
return Collections.emptyMap();
|
return Collections.emptyMap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,7 +98,7 @@ public class TQueryExecutorTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, String> getProperties() {
|
public Map<String, String> getQueryHints() {
|
||||||
return Collections.emptyMap();
|
return Collections.emptyMap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@ public class TPQueryPropertyTest {
|
||||||
.build();
|
.build();
|
||||||
Map<String,Object> para = new HashMap<String,Object>();
|
Map<String,Object> para = new HashMap<String,Object>();
|
||||||
TPQExecutorStatement prepared = store.getQueryExecutorContext().prepareQuery("junit.test", para);
|
TPQExecutorStatement prepared = store.getQueryExecutorContext().prepareQuery("junit.test", para);
|
||||||
assertEquals("v0",prepared.getProperties().get("p0"));
|
assertEquals("v0",prepared.getQueryHints().get("p0"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue