From 23da6f3de89e670c8572d29ce63acbe8d2ea6681 Mon Sep 17 00:00:00 2001 From: rbygrave Date: Sat, 9 May 2015 01:12:21 +1200 Subject: [PATCH] No effective change - change newline char --- .../ebeaninternal/server/util/ArrayStack.java | 176 +++---- .../server/util/BeanCollectionFactory.java | 150 +++--- .../server/util/BeanCollectionParams.java | 52 +-- .../server/util/BindParamsParser.java | 436 +++++++++--------- .../server/util/InternalAssert.java | 50 +- 5 files changed, 432 insertions(+), 432 deletions(-) diff --git a/src/main/java/com/avaje/ebeaninternal/server/util/ArrayStack.java b/src/main/java/com/avaje/ebeaninternal/server/util/ArrayStack.java index 1c8261ac2..57ca3cc06 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/util/ArrayStack.java +++ b/src/main/java/com/avaje/ebeaninternal/server/util/ArrayStack.java @@ -1,88 +1,88 @@ -package com.avaje.ebeaninternal.server.util; - -import java.util.ArrayList; -import java.util.EmptyStackException; - -/** - * Stack based on ArrayList. - * - * @author rbygrave - */ -public class ArrayStack { - - private final ArrayList list; - - /** - * Creates an empty Stack with an initial size. - */ - public ArrayStack(int size) { - this.list = new ArrayList(size); - } - - /** - * Creates an empty Stack. - */ - public ArrayStack() { - this.list = new ArrayList(); - } - - /** - * Pushes an item onto the top of this stack. - */ - public E push(E item) { - list.add(item); - return item; - } - - /** - * Removes the object at the top of this stack and returns that object as - * the value of this function. - */ - public E pop() { - int len = list.size(); - E obj = peek(); - list.remove(len - 1); - return obj; - } - - protected E peekZero(boolean retNull) { - int len = list.size(); - if (len == 0) { - if (retNull) { - return null; - } - throw new EmptyStackException(); - } - return list.get(len - 1); - } - - /** - * Returns the object at the top of this stack without removing it. - */ - public E peek() { - return peekZero(false); - } - - /** - * Returns the object at the top of this stack without removing it. - * If the stack is empty this returns null. - */ - public E peekWithNull() { - return peekZero(true); - } - - /** - * Tests if this stack is empty. - */ - public boolean isEmpty() { - return list.isEmpty(); - } - - public int size(){ - return list.size(); - } - - public boolean contains(Object o){ - return list.contains(o); - } -} +package com.avaje.ebeaninternal.server.util; + +import java.util.ArrayList; +import java.util.EmptyStackException; + +/** + * Stack based on ArrayList. + * + * @author rbygrave + */ +public class ArrayStack { + + private final ArrayList list; + + /** + * Creates an empty Stack with an initial size. + */ + public ArrayStack(int size) { + this.list = new ArrayList(size); + } + + /** + * Creates an empty Stack. + */ + public ArrayStack() { + this.list = new ArrayList(); + } + + /** + * Pushes an item onto the top of this stack. + */ + public E push(E item) { + list.add(item); + return item; + } + + /** + * Removes the object at the top of this stack and returns that object as + * the value of this function. + */ + public E pop() { + int len = list.size(); + E obj = peek(); + list.remove(len - 1); + return obj; + } + + protected E peekZero(boolean retNull) { + int len = list.size(); + if (len == 0) { + if (retNull) { + return null; + } + throw new EmptyStackException(); + } + return list.get(len - 1); + } + + /** + * Returns the object at the top of this stack without removing it. + */ + public E peek() { + return peekZero(false); + } + + /** + * Returns the object at the top of this stack without removing it. + * If the stack is empty this returns null. + */ + public E peekWithNull() { + return peekZero(true); + } + + /** + * Tests if this stack is empty. + */ + public boolean isEmpty() { + return list.isEmpty(); + } + + public int size(){ + return list.size(); + } + + public boolean contains(Object o){ + return list.contains(o); + } +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/util/BeanCollectionFactory.java b/src/main/java/com/avaje/ebeaninternal/server/util/BeanCollectionFactory.java index d7ad1b23f..3a7ba14a7 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/util/BeanCollectionFactory.java +++ b/src/main/java/com/avaje/ebeaninternal/server/util/BeanCollectionFactory.java @@ -1,75 +1,75 @@ -package com.avaje.ebeaninternal.server.util; - -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; - -import com.avaje.ebean.bean.BeanCollection; -import com.avaje.ebean.common.BeanList; -import com.avaje.ebean.common.BeanMap; -import com.avaje.ebean.common.BeanSet; -import com.avaje.ebeaninternal.api.SpiQuery; - -/** - * Creates the BeanCollections. - *

- * Creates the BeanSet BeanMap and BeanList objects. - *

- */ -public class BeanCollectionFactory { - - private static class BeanCollectionFactoryHolder { - private static BeanCollectionFactory me = new BeanCollectionFactory(); - } - - private static final int defaultListInitialCapacity = 20; - private static final int defaultSetInitialCapacity = 32; - private static final int defaultMapInitialCapacity = 32; - - private BeanCollectionFactory() { - - } - - /** - * Create a BeanCollection for the given parameters. - */ - public static BeanCollection create(BeanCollectionParams params) { - return BeanCollectionFactoryHolder.me.createMany(params); - } - - - private BeanCollection createMany(BeanCollectionParams params) { - - SpiQuery.Type manyType = params.getManyType(); - switch (manyType) { - case MAP: - return createMap(params); - case LIST: - return createList(params); - case SET: - return createSet(params); - - default: - throw new RuntimeException("Invalid Arg " + manyType); - } - - } - - @SuppressWarnings({ "unchecked", "rawtypes" }) - private BeanMap createMap(BeanCollectionParams params) { - - return new BeanMap(new LinkedHashMap(defaultMapInitialCapacity)); - } - - @SuppressWarnings({ "unchecked", "rawtypes" }) - private BeanSet createSet(BeanCollectionParams params) { - - return new BeanSet(new LinkedHashSet(defaultSetInitialCapacity)); - } - - @SuppressWarnings({ "unchecked", "rawtypes" }) - private BeanList createList(BeanCollectionParams params) { - - return new BeanList(new ArrayList(defaultListInitialCapacity)); - } -} +package com.avaje.ebeaninternal.server.util; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; + +import com.avaje.ebean.bean.BeanCollection; +import com.avaje.ebean.common.BeanList; +import com.avaje.ebean.common.BeanMap; +import com.avaje.ebean.common.BeanSet; +import com.avaje.ebeaninternal.api.SpiQuery; + +/** + * Creates the BeanCollections. + *

+ * Creates the BeanSet BeanMap and BeanList objects. + *

+ */ +public class BeanCollectionFactory { + + private static class BeanCollectionFactoryHolder { + private static BeanCollectionFactory me = new BeanCollectionFactory(); + } + + private static final int defaultListInitialCapacity = 20; + private static final int defaultSetInitialCapacity = 32; + private static final int defaultMapInitialCapacity = 32; + + private BeanCollectionFactory() { + + } + + /** + * Create a BeanCollection for the given parameters. + */ + public static BeanCollection create(BeanCollectionParams params) { + return BeanCollectionFactoryHolder.me.createMany(params); + } + + + private BeanCollection createMany(BeanCollectionParams params) { + + SpiQuery.Type manyType = params.getManyType(); + switch (manyType) { + case MAP: + return createMap(params); + case LIST: + return createList(params); + case SET: + return createSet(params); + + default: + throw new RuntimeException("Invalid Arg " + manyType); + } + + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + private BeanMap createMap(BeanCollectionParams params) { + + return new BeanMap(new LinkedHashMap(defaultMapInitialCapacity)); + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + private BeanSet createSet(BeanCollectionParams params) { + + return new BeanSet(new LinkedHashSet(defaultSetInitialCapacity)); + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + private BeanList createList(BeanCollectionParams params) { + + return new BeanList(new ArrayList(defaultListInitialCapacity)); + } +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/util/BeanCollectionParams.java b/src/main/java/com/avaje/ebeaninternal/server/util/BeanCollectionParams.java index 91dbb19ac..9fd3c1c94 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/util/BeanCollectionParams.java +++ b/src/main/java/com/avaje/ebeaninternal/server/util/BeanCollectionParams.java @@ -1,26 +1,26 @@ -package com.avaje.ebeaninternal.server.util; - -import com.avaje.ebeaninternal.api.SpiQuery; - -/** - * Parameters used to create the specific Map Set or List object. - */ -public class BeanCollectionParams { - - private final SpiQuery.Type manyType; - - /** - * Construct without a specific capacity. - */ - public BeanCollectionParams(SpiQuery.Type manyType) { - this.manyType = manyType; - } - - /** - * Return the type Map Set or List. - */ - public SpiQuery.Type getManyType() { - return manyType; - } - -} +package com.avaje.ebeaninternal.server.util; + +import com.avaje.ebeaninternal.api.SpiQuery; + +/** + * Parameters used to create the specific Map Set or List object. + */ +public class BeanCollectionParams { + + private final SpiQuery.Type manyType; + + /** + * Construct without a specific capacity. + */ + public BeanCollectionParams(SpiQuery.Type manyType) { + this.manyType = manyType; + } + + /** + * Return the type Map Set or List. + */ + public SpiQuery.Type getManyType() { + return manyType; + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/util/BindParamsParser.java b/src/main/java/com/avaje/ebeaninternal/server/util/BindParamsParser.java index 56ef81325..ed2918e50 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/util/BindParamsParser.java +++ b/src/main/java/com/avaje/ebeaninternal/server/util/BindParamsParser.java @@ -1,218 +1,218 @@ -package com.avaje.ebeaninternal.server.util; - -import java.util.Collection; - -import javax.persistence.PersistenceException; - -import com.avaje.ebean.config.EncryptKey; -import com.avaje.ebeaninternal.api.BindParams; -import com.avaje.ebeaninternal.api.BindParams.OrderedList; -import com.avaje.ebeaninternal.api.BindParams.Param; -import com.avaje.ebeaninternal.server.deploy.BeanDescriptor; - -/** - * Parses the BindParams if they are using named parameters. - *

- * This is a thread safe implementation. - *

- */ -public class BindParamsParser { - - public static final String ENCRYPTKEY_PREFIX = "encryptkey_"; - public static final String ENCRYPTKEY_GAP = "___"; - - private static final int ENCRYPTKEY_PREFIX_LEN = ENCRYPTKEY_PREFIX.length(); - private static final int ENCRYPTKEY_GAP_LEN = ENCRYPTKEY_GAP.length(); - - /** - * Used to parse sql looking for named parameters. - */ - private static final String quote = "'"; - - /** - * Used to parse sql looking for named parameters. - */ - private static final String colon = ":"; - - private final BindParams params; - private final String sql; - - private final BeanDescriptor beanDescriptor; - - public static String parse(BindParams params, String sql) { - return parse(params, sql, null); - } - - public static String parse(BindParams params, String sql, BeanDescriptor beanDescriptor) { - return new BindParamsParser(params, sql, beanDescriptor).parseSql(); - } - - public static OrderedList parseNamedParams(BindParams params, String sql) { - return new BindParamsParser(params, sql, null).parseSqlNamedParams(); - } - - private BindParamsParser(BindParams params, String sql, BeanDescriptor beanDescriptor) { - this.params = params; - this.sql = sql; - this.beanDescriptor = beanDescriptor; - } - - /** - * Used for parsing having clauses with named parameters. - *

- * The issue here is that BindParams contains named parameters for - * both where and having clauses. BindParams.positionedParameters is - * used for the where and the OrderedList for the having. - *

- */ - private OrderedList parseSqlNamedParams() { - OrderedList orderedList = new OrderedList(); - parseNamedParams(orderedList); - return orderedList; - } - - /** - * Parse the sql changed named parameters to positioned parameters if required. - *

- * The sql is used when named parameters are used. - *

- *

- * This is used in most cases of named parameters. The case it is NOT used for is - * named parameters in a having clause. In this case some of the named parameters - * could be for a where clause and some for the having clause. - *

- */ - private String parseSql() { - - if (params.isSameBindHash()) { - String preparedSql = params.getPreparedSql(); - if (preparedSql != null && preparedSql.length() > 0) { - // the sql has already been parsed and positionedParameters are set in order - return preparedSql; - } - } - - String preparedSql; - if (params.requiresNamedParamsPrepare()) { - // convert named parameters into ordered list - OrderedList orderedList = params.createOrderedList(); - parseNamedParams(orderedList); - preparedSql = orderedList.getPreparedSql(); - } else { - preparedSql = sql; - } - params.setPreparedSql(preparedSql); - return preparedSql; - } - - /** - * Named parameters need to be parsed and replaced with ?. - */ - private void parseNamedParams(OrderedList orderedList) { - - parseNamedParams(0, orderedList); - } - - private void parseNamedParams(int startPos, OrderedList orderedList) { - - if (sql == null) { - throw new PersistenceException("query does not contain any named bind parameters?"); - } - if (startPos > sql.length()) { - return; - } - - // search for quotes and named params... in order... - int beginQuotePos = sql.indexOf(quote, startPos); - int nameParamStart = sql.indexOf(colon, startPos); - if (beginQuotePos > 0 && beginQuotePos < nameParamStart) { - // the quote precedes the named parameter... - // find and add up to the end quote - int endQuotePos = sql.indexOf(quote, beginQuotePos + 1); - String sub = sql.substring(startPos, endQuotePos + 1); - orderedList.appendSql(sub); - - // start again after the end quote - parseNamedParams(endQuotePos + 1, orderedList); - - } else { - if (nameParamStart < 0) { - // no more params, add the rest - String sub = sql.substring(startPos, sql.length()); - orderedList.appendSql(sub); - - } else { - // find the end of the parameter name - int endOfParam = nameParamStart + 1; - do { - char c = sql.charAt(endOfParam); - if (c != '_' && !Character.isLetterOrDigit(c)) { - break; - } - endOfParam++; - } while (endOfParam < sql.length()); - - // add the named parameter value to bindList - String paramName = sql.substring(nameParamStart + 1, endOfParam); - - Param param; - if (paramName.startsWith(ENCRYPTKEY_PREFIX)) { - param = addEncryptKeyParam(paramName); - } else { - param = params.getParameter(paramName); - } - - if (param == null) { - String msg = "Bind value is not set or null for [" + paramName + "] in [" + sql + "]"; - throw new PersistenceException(msg); - } - - String sub = sql.substring(startPos, nameParamStart); - orderedList.appendSql(sub); - - // check if inValue is a Collection type... - Object inValue = param.getInValue(); - if (inValue != null && inValue instanceof Collection) { - // Chop up Collection parameter into a number - // of individual parameters and add each one individually - Collection collection = (Collection) inValue; - int c = 0; - for (Object elVal : collection) { - if (++c > 1) { - orderedList.appendSql(","); - } - orderedList.appendSql("?"); - BindParams.Param elParam = new BindParams.Param(); - elParam.setInValue(elVal); - orderedList.add(elParam); - } - - } else { - // its a normal scalar value parameter... - orderedList.add(param); - orderedList.appendSql("?"); - } - - // continue on after the end of the parameter - parseNamedParams(endOfParam, orderedList); - } - } - } - - /** - * Add an encryption key bind parameter. - */ - private Param addEncryptKeyParam(String keyNamedParam) { - - int pos = keyNamedParam.indexOf(ENCRYPTKEY_GAP, ENCRYPTKEY_PREFIX_LEN); - - String tableName = keyNamedParam.substring(ENCRYPTKEY_PREFIX_LEN, pos); - String columnName = keyNamedParam.substring(pos + ENCRYPTKEY_GAP_LEN); - - EncryptKey key = beanDescriptor.getEncryptKey(tableName, columnName); - String strKey = key.getStringValue(); - - return params.setEncryptionKey(keyNamedParam, strKey); - } - -} +package com.avaje.ebeaninternal.server.util; + +import java.util.Collection; + +import javax.persistence.PersistenceException; + +import com.avaje.ebean.config.EncryptKey; +import com.avaje.ebeaninternal.api.BindParams; +import com.avaje.ebeaninternal.api.BindParams.OrderedList; +import com.avaje.ebeaninternal.api.BindParams.Param; +import com.avaje.ebeaninternal.server.deploy.BeanDescriptor; + +/** + * Parses the BindParams if they are using named parameters. + *

+ * This is a thread safe implementation. + *

+ */ +public class BindParamsParser { + + public static final String ENCRYPTKEY_PREFIX = "encryptkey_"; + public static final String ENCRYPTKEY_GAP = "___"; + + private static final int ENCRYPTKEY_PREFIX_LEN = ENCRYPTKEY_PREFIX.length(); + private static final int ENCRYPTKEY_GAP_LEN = ENCRYPTKEY_GAP.length(); + + /** + * Used to parse sql looking for named parameters. + */ + private static final String quote = "'"; + + /** + * Used to parse sql looking for named parameters. + */ + private static final String colon = ":"; + + private final BindParams params; + private final String sql; + + private final BeanDescriptor beanDescriptor; + + public static String parse(BindParams params, String sql) { + return parse(params, sql, null); + } + + public static String parse(BindParams params, String sql, BeanDescriptor beanDescriptor) { + return new BindParamsParser(params, sql, beanDescriptor).parseSql(); + } + + public static OrderedList parseNamedParams(BindParams params, String sql) { + return new BindParamsParser(params, sql, null).parseSqlNamedParams(); + } + + private BindParamsParser(BindParams params, String sql, BeanDescriptor beanDescriptor) { + this.params = params; + this.sql = sql; + this.beanDescriptor = beanDescriptor; + } + + /** + * Used for parsing having clauses with named parameters. + *

+ * The issue here is that BindParams contains named parameters for + * both where and having clauses. BindParams.positionedParameters is + * used for the where and the OrderedList for the having. + *

+ */ + private OrderedList parseSqlNamedParams() { + OrderedList orderedList = new OrderedList(); + parseNamedParams(orderedList); + return orderedList; + } + + /** + * Parse the sql changed named parameters to positioned parameters if required. + *

+ * The sql is used when named parameters are used. + *

+ *

+ * This is used in most cases of named parameters. The case it is NOT used for is + * named parameters in a having clause. In this case some of the named parameters + * could be for a where clause and some for the having clause. + *

+ */ + private String parseSql() { + + if (params.isSameBindHash()) { + String preparedSql = params.getPreparedSql(); + if (preparedSql != null && preparedSql.length() > 0) { + // the sql has already been parsed and positionedParameters are set in order + return preparedSql; + } + } + + String preparedSql; + if (params.requiresNamedParamsPrepare()) { + // convert named parameters into ordered list + OrderedList orderedList = params.createOrderedList(); + parseNamedParams(orderedList); + preparedSql = orderedList.getPreparedSql(); + } else { + preparedSql = sql; + } + params.setPreparedSql(preparedSql); + return preparedSql; + } + + /** + * Named parameters need to be parsed and replaced with ?. + */ + private void parseNamedParams(OrderedList orderedList) { + + parseNamedParams(0, orderedList); + } + + private void parseNamedParams(int startPos, OrderedList orderedList) { + + if (sql == null) { + throw new PersistenceException("query does not contain any named bind parameters?"); + } + if (startPos > sql.length()) { + return; + } + + // search for quotes and named params... in order... + int beginQuotePos = sql.indexOf(quote, startPos); + int nameParamStart = sql.indexOf(colon, startPos); + if (beginQuotePos > 0 && beginQuotePos < nameParamStart) { + // the quote precedes the named parameter... + // find and add up to the end quote + int endQuotePos = sql.indexOf(quote, beginQuotePos + 1); + String sub = sql.substring(startPos, endQuotePos + 1); + orderedList.appendSql(sub); + + // start again after the end quote + parseNamedParams(endQuotePos + 1, orderedList); + + } else { + if (nameParamStart < 0) { + // no more params, add the rest + String sub = sql.substring(startPos, sql.length()); + orderedList.appendSql(sub); + + } else { + // find the end of the parameter name + int endOfParam = nameParamStart + 1; + do { + char c = sql.charAt(endOfParam); + if (c != '_' && !Character.isLetterOrDigit(c)) { + break; + } + endOfParam++; + } while (endOfParam < sql.length()); + + // add the named parameter value to bindList + String paramName = sql.substring(nameParamStart + 1, endOfParam); + + Param param; + if (paramName.startsWith(ENCRYPTKEY_PREFIX)) { + param = addEncryptKeyParam(paramName); + } else { + param = params.getParameter(paramName); + } + + if (param == null) { + String msg = "Bind value is not set or null for [" + paramName + "] in [" + sql + "]"; + throw new PersistenceException(msg); + } + + String sub = sql.substring(startPos, nameParamStart); + orderedList.appendSql(sub); + + // check if inValue is a Collection type... + Object inValue = param.getInValue(); + if (inValue != null && inValue instanceof Collection) { + // Chop up Collection parameter into a number + // of individual parameters and add each one individually + Collection collection = (Collection) inValue; + int c = 0; + for (Object elVal : collection) { + if (++c > 1) { + orderedList.appendSql(","); + } + orderedList.appendSql("?"); + BindParams.Param elParam = new BindParams.Param(); + elParam.setInValue(elVal); + orderedList.add(elParam); + } + + } else { + // its a normal scalar value parameter... + orderedList.add(param); + orderedList.appendSql("?"); + } + + // continue on after the end of the parameter + parseNamedParams(endOfParam, orderedList); + } + } + } + + /** + * Add an encryption key bind parameter. + */ + private Param addEncryptKeyParam(String keyNamedParam) { + + int pos = keyNamedParam.indexOf(ENCRYPTKEY_GAP, ENCRYPTKEY_PREFIX_LEN); + + String tableName = keyNamedParam.substring(ENCRYPTKEY_PREFIX_LEN, pos); + String columnName = keyNamedParam.substring(pos + ENCRYPTKEY_GAP_LEN); + + EncryptKey key = beanDescriptor.getEncryptKey(tableName, columnName); + String strKey = key.getStringValue(); + + return params.setEncryptionKey(keyNamedParam, strKey); + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/util/InternalAssert.java b/src/main/java/com/avaje/ebeaninternal/server/util/InternalAssert.java index 6ea1d1283..7f09d9afc 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/util/InternalAssert.java +++ b/src/main/java/com/avaje/ebeaninternal/server/util/InternalAssert.java @@ -1,25 +1,25 @@ -package com.avaje.ebeaninternal.server.util; - -/** - * Helper for checking the state of the application is correct. - */ -public class InternalAssert { - - /** - * Throws an IllegalStateException if o is null. - */ - public static void notNull(Object o, String msg){ - if (o == null){ - throw new IllegalStateException(msg); - } - } - - /** - * Throws an IllegalStateException if b is not true. - */ - public static void isTrue(boolean b, String msg){ - if (!b){ - throw new IllegalStateException(msg); - } - } -} +package com.avaje.ebeaninternal.server.util; + +/** + * Helper for checking the state of the application is correct. + */ +public class InternalAssert { + + /** + * Throws an IllegalStateException if o is null. + */ + public static void notNull(Object o, String msg){ + if (o == null){ + throw new IllegalStateException(msg); + } + } + + /** + * Throws an IllegalStateException if b is not true. + */ + public static void isTrue(boolean b, String msg){ + if (!b){ + throw new IllegalStateException(msg); + } + } +}