From f83bc54958e3211240ade0b77d93c3c9fd0aa3d0 Mon Sep 17 00:00:00 2001
From: Nikolaus Krismer <nikolaus.krismer@uibk.ac.at>
Date: Tue, 26 Aug 2014 13:47:43 +0200
Subject: [PATCH] fixed problem in placeHolder caching

---
 .../it/unibz/inf/isochrone/db/Database.java   | 65 +++++++++++--------
 1 file changed, 39 insertions(+), 26 deletions(-)

diff --git a/src/main/java/it/unibz/inf/isochrone/db/Database.java b/src/main/java/it/unibz/inf/isochrone/db/Database.java
index 201f307b..1a572ee4 100644
--- a/src/main/java/it/unibz/inf/isochrone/db/Database.java
+++ b/src/main/java/it/unibz/inf/isochrone/db/Database.java
@@ -74,7 +74,7 @@ public class Database {
 		final int preachingLength = 10;
 		PRECACHED_PLACEHOLDERS = new String[preachingLength];
 		for (int i = 0; i < preachingLength; ++i) {
-			PRECACHED_PLACEHOLDERS[i] = preparePlaceHolders(i);
+			PRECACHED_PLACEHOLDERS[i] = calculatePlaceHolders(i);
 		}
 	}
 
@@ -743,31 +743,6 @@ public class Database {
 
 	// Private static methods
 
-	/**
-	 * Prepares a number of place holders for a prepared statement.
-	 *
-	 * @param length The number of placeholders that is needed
-	 * @return a string with the placeholders plugged in to a statement.
-	 */
-	private static String preparePlaceHolders(final int length) {
-		if (length <= 0) {
-			return "";
-		}
-		if (length < PRECACHED_PLACEHOLDERS.length) {
-			return PRECACHED_PLACEHOLDERS[length];
-		}
-
-		final StringBuilder builder = new StringBuilder((2 * length) - 1);
-		builder.append("?");
-
-		final int max = length - 1;
-		for (int i = 0; i < max; ++i) {
-			builder.append(",?");
-		}
-
-		return builder.toString();
-	}
-
 	/**
 	 * Adds a node to the network.
 	 *
@@ -800,6 +775,25 @@ public class Database {
 		}
 	}
 
+	/**
+	 * Calculates a number of place holders for a prepared statement.
+	 * In difference to @see {@link #preparePlaceHolders(int)} this does not use any caching approach at all.
+	 *
+	 * @param length The number of placeholders that is needed
+	 * @return a string with the placeholders plugged in to a statement.
+	 */
+	private static String calculatePlaceHolders(final int length) {
+		final StringBuilder builder = new StringBuilder((2 * length) - 1);
+		builder.append("?");
+
+		final int max = length - 1;
+		for (int i = 0; i < max; ++i) {
+			builder.append(",?");
+		}
+
+		return builder.toString();
+	}
+
 	/**
 	 * Transforms the day into something machine-readable.
 	 *
@@ -827,6 +821,25 @@ public class Database {
 		}
 	}
 
+	/**
+	 * Prepares a number of place holders for a prepared statement.
+	 * In difference to @see {@link #calculatePlaceHolders(int)} this uses a caching approach for small lengths
+	 *
+	 * @param length The number of placeholders that is needed
+	 * @return a string with the placeholders plugged in to a statement.
+	 */
+	private static String preparePlaceHolders(final int length) {
+		if (length <= 0) {
+			return "";
+		}
+
+		if (length < PRECACHED_PLACEHOLDERS.length) {
+			return PRECACHED_PLACEHOLDERS[length];
+		}
+
+		return calculatePlaceHolders(length);
+	}
+
 	private static String subQuery(final Node[] loaded, final int idx, final int srid) {
 		if (idx == 0) {
 			return "st_buffer(st_pointfromtext('POINT(" + loaded[idx].getCoordinates().getX() + " "
-- 
GitLab