From cde978955c2cb86718dbd1dfcac3d9f059d76a5a Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Fri, 30 Mar 2018 12:40:26 +0300 Subject: [PATCH] Get all translations in wikivoyage search --- .../wikivoyage/data/WikivoyageDbHelper.java | 40 +++++++++++++++---- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageDbHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageDbHelper.java index 7e143d8515..051288b40e 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageDbHelper.java @@ -21,6 +21,7 @@ import java.util.Collections; import java.util.Comparator; import java.util.List; +import gnu.trove.list.array.TLongArrayList; import gnu.trove.map.hash.TLongObjectHashMap; public class WikivoyageDbHelper { @@ -78,18 +79,22 @@ public class WikivoyageDbHelper { @NonNull public List search(final String searchQuery) { + TLongArrayList cityIds = getCityIdsBySearchTerm(searchQuery); + List res = new ArrayList<>(); SQLiteConnection conn = openConnection(); if (conn != null) { try { - String dbQuery = SEARCH_TABLE_SELECT + " WHERE " + SEARCH_COL_SEARCH_TERM + " LIKE ?"; - SQLiteCursor cursor = conn.rawQuery(dbQuery, new String[]{searchQuery + "%"}); - if (cursor.moveToFirst()) { - do { - res.add(readSearchResult(cursor)); - } while (cursor.moveToNext()); + for (int i = 0; i < cityIds.size(); i++) { + String dbQuery = SEARCH_TABLE_SELECT + " WHERE " + SEARCH_COL_CITY_ID + " = ?"; + SQLiteCursor cursor = conn.rawQuery(dbQuery, new String[]{String.valueOf(cityIds.get(0))}); + if (cursor.moveToFirst()) { + do { + res.add(readSearchResult(cursor)); + } while (cursor.moveToNext()); + } + cursor.close(); } - cursor.close(); } finally { conn.close(); } @@ -118,6 +123,27 @@ public class WikivoyageDbHelper { return list; } + private TLongArrayList getCityIdsBySearchTerm(String searchTerm) { + TLongArrayList res = new TLongArrayList(); + SQLiteConnection conn = openConnection(); + if (conn != null) { + try { + String query = "SELECT DISTINCT " + SEARCH_COL_CITY_ID + " FROM " + SEARCH_TABLE_NAME + + " WHERE " + SEARCH_COL_SEARCH_TERM + " LIKE ?"; + SQLiteCursor cursor = conn.rawQuery(query, new String[]{searchTerm + "%"}); + if (cursor.moveToFirst()) { + do { + res.add(cursor.getLong(0)); + } while (cursor.moveToNext()); + } + cursor.close(); + } finally { + conn.close(); + } + } + return res; + } + private Collection groupSearchResultsByCityId(List res) { String baseLng = application.getLanguage(); TLongObjectHashMap wikivoyage = new TLongObjectHashMap<>();