Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/data/NextGenDB.java
Show First 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | private static final String SQL_CREATE_STOPS_TABLE="CREATE TABLE "+Contract.StopsTable.TABLE_NAME+" ("+ | ||||
Contract.StopsTable.COL_LINES_STOPPING +" TEXT )"; | Contract.StopsTable.COL_LINES_STOPPING +" TEXT )"; | ||||
private static final String SQL_CREATE_STOPS_TABLE_TO_COMPLETE = " ("+ | private static final String SQL_CREATE_STOPS_TABLE_TO_COMPLETE = " ("+ | ||||
Contract.StopsTable.COL_ID+" TEXT PRIMARY KEY, "+ Contract.StopsTable.COL_TYPE+" INTEGER, "+Contract.StopsTable.COL_LAT+" REAL NOT NULL, "+ | Contract.StopsTable.COL_ID+" TEXT PRIMARY KEY, "+ Contract.StopsTable.COL_TYPE+" INTEGER, "+Contract.StopsTable.COL_LAT+" REAL NOT NULL, "+ | ||||
Contract.StopsTable.COL_LONG+" REAL NOT NULL, "+ Contract.StopsTable.COL_NAME+" TEXT NOT NULL, "+ | Contract.StopsTable.COL_LONG+" REAL NOT NULL, "+ Contract.StopsTable.COL_NAME+" TEXT NOT NULL, "+ | ||||
Contract.StopsTable.COL_LOCATION+" TEXT, "+Contract.StopsTable.COL_PLACE+" TEXT, "+ | Contract.StopsTable.COL_LOCATION+" TEXT, "+Contract.StopsTable.COL_PLACE+" TEXT, "+ | ||||
Contract.StopsTable.COL_LINES_STOPPING +" TEXT )"; | Contract.StopsTable.COL_LINES_STOPPING +" TEXT )"; | ||||
private static final String[] QUERY_COLUMN_stops_all = { | public static final String[] QUERY_COLUMN_stops_all = { | ||||
StopsTable.COL_ID, StopsTable.COL_NAME, StopsTable.COL_LOCATION, | StopsTable.COL_ID, StopsTable.COL_NAME, StopsTable.COL_LOCATION, | ||||
StopsTable.COL_TYPE, StopsTable.COL_LAT, StopsTable.COL_LONG, StopsTable.COL_LINES_STOPPING}; | StopsTable.COL_TYPE, StopsTable.COL_LAT, StopsTable.COL_LONG, StopsTable.COL_LINES_STOPPING}; | ||||
private static final String QUERY_WHERE_LAT_AND_LNG_IN_RANGE = StopsTable.COL_LAT + " >= ? AND " + | public static final String QUERY_WHERE_LAT_AND_LNG_IN_RANGE = StopsTable.COL_LAT + " >= ? AND " + | ||||
StopsTable.COL_LAT + " <= ? AND "+ StopsTable.COL_LONG + | StopsTable.COL_LAT + " <= ? AND "+ StopsTable.COL_LONG + | ||||
" >= ? AND "+ StopsTable.COL_LONG + " <= ?"; | " >= ? AND "+ StopsTable.COL_LONG + " <= ?"; | ||||
public static String QUERY_WHERE_ID = StopsTable.COL_ID+" = ?"; | |||||
private Context appContext; | |||||
private final Context appContext; | |||||
public NextGenDB(Context context) { | public NextGenDB(Context context) { | ||||
super(context, DATABASE_NAME, null, DATABASE_VERSION); | super(context, DATABASE_NAME, null, DATABASE_VERSION); | ||||
appContext = context.getApplicationContext(); | appContext = context.getApplicationContext(); | ||||
} | } | ||||
@Override | @Override | ||||
▲ Show 20 Lines • Show All 62 Lines • ▼ Show 20 Lines | public synchronized Stop[] queryAllInsideMapView(double minLat, double maxLat, double minLng, double maxLng) { | ||||
int count; | int count; | ||||
// coordinates must be strings in the where condition | // coordinates must be strings in the where condition | ||||
String minLatRaw = String.valueOf(minLat); | String minLatRaw = String.valueOf(minLat); | ||||
String maxLatRaw = String.valueOf(maxLat); | String maxLatRaw = String.valueOf(maxLat); | ||||
String minLngRaw = String.valueOf(minLng); | String minLngRaw = String.valueOf(minLng); | ||||
String maxLngRaw = String.valueOf(maxLng); | String maxLngRaw = String.valueOf(maxLng); | ||||
String[] queryColumns = {}; | |||||
String stopID; | |||||
Route.Type type; | |||||
if(db == null) { | if(db == null) { | ||||
return stops; | return stops; | ||||
} | } | ||||
try { | try { | ||||
result = db.query(StopsTable.TABLE_NAME, QUERY_COLUMN_stops_all, QUERY_WHERE_LAT_AND_LNG_IN_RANGE, | result = db.query(StopsTable.TABLE_NAME, QUERY_COLUMN_stops_all, QUERY_WHERE_LAT_AND_LNG_IN_RANGE, | ||||
new String[] {minLatRaw, maxLatRaw, minLngRaw, maxLngRaw}, | new String[] {minLatRaw, maxLatRaw, minLngRaw, maxLngRaw}, | ||||
null, null, null); | null, null, null); | ||||
stops = getStopsFromCursorAllFields(result); | |||||
} catch(SQLiteException e) { | |||||
Log.e(DEBUG_TAG, "SQLiteException occurred"); | |||||
e.printStackTrace(); | |||||
return stops; | |||||
} | |||||
result.close(); | |||||
db.close(); | |||||
return stops; | |||||
} | |||||
/** | |||||
* Get the list of stop in the query, with all the possible fields {NextGenDB.QUERY_COLUMN_stops_all} | |||||
* @param result cursor from query | |||||
* @return an Array of the stops found in the query | |||||
*/ | |||||
public static Stop[] getStopsFromCursorAllFields(Cursor result){ | |||||
int colID = result.getColumnIndex(StopsTable.COL_ID); | int colID = result.getColumnIndex(StopsTable.COL_ID); | ||||
int colName = result.getColumnIndex(StopsTable.COL_NAME); | int colName = result.getColumnIndex(StopsTable.COL_NAME); | ||||
int colLocation = result.getColumnIndex(StopsTable.COL_LOCATION); | int colLocation = result.getColumnIndex(StopsTable.COL_LOCATION); | ||||
int colType = result.getColumnIndex(StopsTable.COL_TYPE); | int colType = result.getColumnIndex(StopsTable.COL_TYPE); | ||||
int colLat = result.getColumnIndex(StopsTable.COL_LAT); | int colLat = result.getColumnIndex(StopsTable.COL_LAT); | ||||
int colLon = result.getColumnIndex(StopsTable.COL_LONG); | int colLon = result.getColumnIndex(StopsTable.COL_LONG); | ||||
int colLines = result.getColumnIndex(StopsTable.COL_LINES_STOPPING); | int colLines = result.getColumnIndex(StopsTable.COL_LINES_STOPPING); | ||||
count = result.getCount(); | int count = result.getCount(); | ||||
stops = new Stop[count]; | Stop[] stops = new Stop[count]; | ||||
int i = 0; | int i = 0; | ||||
while(result.moveToNext()) { | while(result.moveToNext()) { | ||||
stopID = result.getString(colID); | final String stopID = result.getString(colID).trim(); | ||||
type = Route.getTypeFromSymbol(result.getString(colType)); | final Route.Type type = Route.getTypeFromSymbol(result.getString(colType)); | ||||
String lines = result.getString(colLines).trim(); | String lines = result.getString(colLines).trim(); | ||||
String locationSometimesEmpty = result.getString(colLocation); | String locationSometimesEmpty = result.getString(colLocation); | ||||
if (locationSometimesEmpty!= null && locationSometimesEmpty.length() <= 0) { | if (locationSometimesEmpty!= null && locationSometimesEmpty.length() <= 0) { | ||||
locationSometimesEmpty = null; | locationSometimesEmpty = null; | ||||
} | } | ||||
stops[i++] = new Stop(stopID, result.getString(colName), null, | stops[i++] = new Stop(stopID, result.getString(colName), null, | ||||
locationSometimesEmpty, type, splitLinesString(lines), | locationSometimesEmpty, type, splitLinesString(lines), | ||||
result.getDouble(colLat), result.getDouble(colLon)); | result.getDouble(colLat), result.getDouble(colLon)); | ||||
} | } | ||||
} catch(SQLiteException e) { | |||||
Log.e(DEBUG_TAG, "SQLiteException occurred"); | |||||
e.printStackTrace(); | |||||
return stops; | |||||
} | |||||
result.close(); | |||||
db.close(); | |||||
return stops; | return stops; | ||||
} | } | ||||
/** | /** | ||||
* Insert batch content, already prepared as | * Insert batch content, already prepared as | ||||
* @param content ContentValues array | * @param content ContentValues array | ||||
* @return number of lines inserted | * @return number of lines inserted | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 124 Lines • Show Last 20 Lines |
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register