postgresql-jdbc.git / summary / log / commit / refs

commit    a1029dff03852a273ba216ce00a5ae33c1c851f9
Author:   Vladimir Sitnikov <sitnikov.vladimir@gmail.com>
Date:     Mon Jun 29 06:18:34 2015 +0000

    refactor: cleanup Parser and NativeQuery after #311
    
    Rename toIntList to toIntArray, use better estimation for NativeQuery.toString result size.


org/postgresql/core/NativeQuery.java | 16 +++++++++++----- org/postgresql/core/Parser.java | 11 ++++++++--- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/org/postgresql/core/NativeQuery.java b/org/postgresql/core/NativeQuery.java index 1aab5716..5870f8d0 100644 --- a/org/postgresql/core/NativeQuery.java +++ b/org/postgresql/core/NativeQuery.java @@ -52,14 +52,20 @@ public class NativeQuery { if (bindPositions.length == 0) return nativeSql; - StringBuilder sbuf = new StringBuilder(nativeSql.length()); + int queryLength = nativeSql.length(); + String[] params = new String[bindPositions.length]; + for (int i = 1; i <= bindPositions.length; ++i) + { + String param = parameters == null ? "?" : parameters.toString(i); + params[i - 1] = param; + queryLength += param.length() - bindName(i).length(); + } + + StringBuilder sbuf = new StringBuilder(queryLength); sbuf.append(nativeSql, 0, bindPositions[0]); for (int i = 1; i <= bindPositions.length; ++i) { - if (parameters == null) - sbuf.append('?'); - else - sbuf.append(parameters.toString(i)); + sbuf.append(params[i - 1]); int nextBind = i < bindPositions.length ? bindPositions[i] : nativeSql.length(); sbuf.append(nativeSql, bindPositions[i - 1] + bindName(i).length(), nextBind); } diff --git a/org/postgresql/core/Parser.java b/org/postgresql/core/Parser.java index 41007a9b..e6d12fad 100644 --- a/org/postgresql/core/Parser.java +++ b/org/postgresql/core/Parser.java @@ -105,7 +105,7 @@ public class Parser { if (nativeQueries == null) nativeQueries = new ArrayList<NativeQuery>(); - nativeQueries.add(new NativeQuery(nativeSql.toString(), toIntList(bindPositions))); + nativeQueries.add(new NativeQuery(nativeSql.toString(), toIntArray(bindPositions))); } // Prepare for next query if (bindPositions != null) @@ -125,7 +125,7 @@ public class Parser { if (nativeSql.length() == 0) return nativeQueries != null ? nativeQueries : Collections.<NativeQuery>emptyList(); - NativeQuery lastQuery = new NativeQuery(nativeSql.toString(), toIntList(bindPositions)); + NativeQuery lastQuery = new NativeQuery(nativeSql.toString(), toIntArray(bindPositions)); if (nativeQueries == null) return Collections.singletonList(lastQuery); @@ -134,7 +134,12 @@ public class Parser { return nativeQueries; } - private static int[] toIntList(List<Integer> list) { + /** + * Converts {@code List<Integer>} to {@code int[]}. Empty and {@code null} lists are converted to empty array. + * @param list input list + * @return output array + */ + private static int[] toIntArray(List<Integer> list) { if (list == null || list.isEmpty()) return NO_BINDS; int[] res = new int[list.size()]; [parent: b8ee75d34c69]