diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2014-01-08 13:26:24 +0100 |
|---|---|---|
| committer | Samuel Tardieu <sam@rfc1149.net> | 2014-01-08 13:50:16 +0100 |
| commit | 20232a47f33653f0950663c4e96467f4de60e303 (patch) | |
| tree | c24cf430c10fd29f96be353fc496a6a98064deb0 | |
| parent | dc577d523fb9ff69f28b94ae1e369e46e778f08f (diff) | |
| download | cgeo-20232a47f33653f0950663c4e96467f4de60e303.zip cgeo-20232a47f33653f0950663c4e96467f4de60e303.tar.gz cgeo-20232a47f33653f0950663c4e96467f4de60e303.tar.bz2 | |
fix #3527: bad query string
Let's hope that the bad formatting occurring for, at least, very small
negative values in `String.format()` with a `null` Locale will not
happen with `StringBuilder.append(double)`.
| -rw-r--r-- | main/src/cgeo/geocaching/DataStore.java | 6 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/geopoint/Viewport.java | 12 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/geopoint/ViewportTest.java | 14 |
3 files changed, 21 insertions, 11 deletions
diff --git a/main/src/cgeo/geocaching/DataStore.java b/main/src/cgeo/geocaching/DataStore.java index bb8b427..0345e30 100644 --- a/main/src/cgeo/geocaching/DataStore.java +++ b/main/src/cgeo/geocaching/DataStore.java @@ -1675,7 +1675,7 @@ public class DataStore { * @return */ - private static String buildCoordinateWhere(final String dbTable, final Viewport viewport) { + private static StringBuilder buildCoordinateWhere(final String dbTable, final Viewport viewport) { return viewport.resize(1.5).sqlWhere(dbTable); } @@ -2265,7 +2265,7 @@ public class DataStore { } // viewport limitation - final StringBuilder selection = new StringBuilder(buildCoordinateWhere(dbTableCaches, viewport)); + final StringBuilder selection = buildCoordinateWhere(dbTableCaches, viewport); // cacheType limitation String[] selectionArgs = null; @@ -2870,7 +2870,7 @@ public class DataStore { */ public static Set<Waypoint> loadWaypoints(final Viewport viewport, boolean excludeMine, boolean excludeDisabled, CacheType type) { - final StringBuilder where = new StringBuilder(buildCoordinateWhere(dbTableWaypoints, viewport)); + final StringBuilder where = buildCoordinateWhere(dbTableWaypoints, viewport); if (excludeMine) { where.append(" and ").append(dbTableCaches).append(".found == 0"); } diff --git a/main/src/cgeo/geocaching/geopoint/Viewport.java b/main/src/cgeo/geocaching/geopoint/Viewport.java index 21dc7fa..9d55f69 100644 --- a/main/src/cgeo/geocaching/geopoint/Viewport.java +++ b/main/src/cgeo/geocaching/geopoint/Viewport.java @@ -3,12 +3,11 @@ package cgeo.geocaching.geopoint; import cgeo.geocaching.ICoordinates; import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; -import java.util.Locale; import java.util.Set; - public class Viewport { public final @NonNull Geopoint center; @@ -104,11 +103,12 @@ public class Viewport { * the database table to use as prefix, or null if no prefix is required * @return the string without the "where" keyword */ - public String sqlWhere(final String dbTable) { + public StringBuilder sqlWhere(@Nullable final String dbTable) { final String prefix = dbTable == null ? "" : (dbTable + "."); - return String.format((Locale) null, - "%slatitude >= %s and %slatitude <= %s and %slongitude >= %s and %slongitude <= %s", - prefix, getLatitudeMin(), prefix, getLatitudeMax(), prefix, getLongitudeMin(), prefix, getLongitudeMax()); + return new StringBuilder(prefix).append("latitude >= ").append(getLatitudeMin()).append(" and ") + .append(prefix).append("latitude <= ").append(getLatitudeMax()).append(" and ") + .append(prefix).append("longitude >= ").append(getLongitudeMin()).append(" and ") + .append(prefix).append("longitude <= ").append(getLongitudeMax()); } /** diff --git a/tests/src/cgeo/geocaching/geopoint/ViewportTest.java b/tests/src/cgeo/geocaching/geopoint/ViewportTest.java index ac32468..60766b4 100644 --- a/tests/src/cgeo/geocaching/geopoint/ViewportTest.java +++ b/tests/src/cgeo/geocaching/geopoint/ViewportTest.java @@ -6,6 +6,7 @@ import android.test.AndroidTestCase; import java.util.Collections; import java.util.HashSet; +import java.util.Locale; import java.util.Set; public class ViewportTest extends AndroidTestCase { @@ -54,8 +55,17 @@ public class ViewportTest extends AndroidTestCase { } public static void testSqlWhere() { - assertEquals("latitude >= -1.0 and latitude <= 3.0 and longitude >= -2.0 and longitude <= 4.0", vpRef.sqlWhere(null)); - assertEquals("t.latitude >= -1.0 and t.latitude <= 3.0 and t.longitude >= -2.0 and t.longitude <= 4.0", vpRef.sqlWhere("t")); + assertEquals("latitude >= -1.0 and latitude <= 3.0 and longitude >= -2.0 and longitude <= 4.0", vpRef.sqlWhere(null).toString()); + assertEquals("t.latitude >= -1.0 and t.latitude <= 3.0 and t.longitude >= -2.0 and t.longitude <= 4.0", vpRef.sqlWhere("t").toString()); + Locale current = null; + try { + current = Locale.getDefault(); + Locale.setDefault(Locale.FRENCH); + assertEquals("1,0", String.format("%.2g", 1.0d)); // Control that we are in a locale with commma separator + assertEquals("t.latitude >= -1.0 and t.latitude <= 3.0 and t.longitude >= -2.0 and t.longitude <= 4.0", vpRef.sqlWhere("t").toString()); + } finally { + Locale.setDefault(current); + } } public static void testEquals() { |
