Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 25 additions & 92 deletions src/main/java/org/mtransit/parser/db/DBUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,17 @@ object DBUtils {
@JvmStatic
fun insertStopTime(gStopTime: GStopTime, preparedStatement: PreparedStatement) {
try {
var idx = 1
var idx = 0
with(preparedStatement) {
setInt(idx++, gStopTime.tripIdInt)
setInt(idx++, gStopTime.stopIdInt)
setInt(idx++, gStopTime.stopSequence)
setInt(idx++, gStopTime.arrivalTime)
setInt(idx++, gStopTime.departureTime)
setString(idx++, gStopTime.stopHeadsign?.quotesEscape())
setInt(idx++, gStopTime.pickupType.id)
setInt(idx++, gStopTime.dropOffType.id)
setInt(idx++, gStopTime.timePoint.id)
setInt(++idx, gStopTime.tripIdInt)
setInt(++idx, gStopTime.stopIdInt)
setInt(++idx, gStopTime.stopSequence)
setInt(++idx, gStopTime.arrivalTime)
setInt(++idx, gStopTime.departureTime)
setString(++idx, gStopTime.stopHeadsign?.quotesEscape())
setInt(++idx, gStopTime.pickupType.id)
setInt(++idx, gStopTime.dropOffType.id)
setInt(++idx, gStopTime.timePoint.id)
addBatch()
}
insertRowCount++
Expand Down Expand Up @@ -416,6 +416,7 @@ object DBUtils {
}
}

@Suppress("AssignedValueIsNeverRead")
@JvmStatic
fun selectSchedules(
serviceIdInt: Int? = null,
Expand All @@ -434,21 +435,11 @@ object DBUtils {
// SERVICE ID
serviceIdInt?.let {
@Suppress("KotlinConstantConditions")
query += if (whereAdded) {
" AND"
} else {
" WHERE"
}
whereAdded = true
query += if (whereAdded) " AND" else " WHERE"; whereAdded = true
query += " ${MSchedule.SERVICE_ID} = $serviceIdInt"
}
serviceIdInts?.let {
query += if (whereAdded) {
" AND"
} else {
" WHERE"
}
whereAdded = true
query += if (whereAdded) " AND" else " WHERE"; whereAdded = true
query += " ${MSchedule.SERVICE_ID} IN ${
serviceIdInts
.distinct()
Expand All @@ -458,26 +449,15 @@ object DBUtils {
postfix = ")"
) { "$it" }
}"
whereAdded = true
}
// DIRECTION ID
directionId?.let {
query += if (whereAdded) {
" AND"
} else {
" WHERE"
}
whereAdded = true
query += if (whereAdded) " AND" else " WHERE"; whereAdded = true
query += " ${MSchedule.DIRECTION_ID} = $directionId"

}
directionIds?.let {
query += if (whereAdded) {
" AND"
} else {
" WHERE"
}
whereAdded = true
query += if (whereAdded) " AND" else " WHERE"; whereAdded = true
query += " ${MSchedule.DIRECTION_ID} IN ${
directionIds
.distinct()
Expand All @@ -487,26 +467,15 @@ object DBUtils {
postfix = ")"
) { "$it" }
}"
whereAdded = true
}
// STOP ID
stopIdInt?.let {
query += if (whereAdded) {
" AND"
} else {
" WHERE"
}
whereAdded = true
query += if (whereAdded) " AND" else " WHERE"; whereAdded = true
query += " ${MSchedule.STOP_ID} = $stopIdInt"

}
stopIdInts?.let {
query += if (whereAdded) {
" AND"
} else {
" WHERE"
}
whereAdded = true
query += if (whereAdded) " AND" else " WHERE"; whereAdded = true
query += " ${MSchedule.STOP_ID} IN ${
stopIdInts
.distinct()
Expand All @@ -516,25 +485,14 @@ object DBUtils {
postfix = ")"
) { "$it" }
}"
whereAdded = true
}
// ARRIVAL & DEPARTURE
arrival?.let {
query += if (whereAdded) {
" AND"
} else {
" WHERE"
}
whereAdded = true
query += if (whereAdded) " AND" else " WHERE"; whereAdded = true
query += " ${MSchedule.ARRIVAL} = $arrival"
}
departure?.let {
query += if (whereAdded) {
" AND"
} else {
" WHERE"
}
whereAdded = true
query += if (whereAdded) " AND" else " WHERE"; whereAdded = true
query += " ${MSchedule.DEPARTURE} = $departure"
}
query += " ORDER BY " +
Expand Down Expand Up @@ -574,7 +532,7 @@ object DBUtils {
}
}

@Suppress("unused")
@Suppress("unused", "AssignedValueIsNeverRead")
@JvmStatic
fun deleteSchedules(
serviceIdInt: Int? = null,
Expand All @@ -587,52 +545,27 @@ object DBUtils {
var whereAdded = false
serviceIdInt?.let {
@Suppress("KotlinConstantConditions")
query += if (whereAdded) {
" AND"
} else {
" WHERE"
}
whereAdded = true
query += if (whereAdded) " AND" else " WHERE"; whereAdded = true
query += " ${MSchedule.SERVICE_ID} = $serviceIdInt"
}
directionId?.let {
query += if (whereAdded) {
" AND"
} else {
" WHERE"
}
whereAdded = true
query += if (whereAdded) " AND" else " WHERE"; whereAdded = true
query += " ${MSchedule.DIRECTION_ID} = $directionId"

}
// STOP ID
stopIdInt?.let {
query += if (whereAdded) {
" AND"
} else {
" WHERE"
}
whereAdded = true
query += if (whereAdded) " AND" else " WHERE"; whereAdded = true
query += " ${MSchedule.STOP_ID} = $stopIdInt"

}
// ARRIVAL & DEPARTURE
arrival?.let {
query += if (whereAdded) {
" AND"
} else {
" WHERE"
}
whereAdded = true
query += if (whereAdded) " AND" else " WHERE"; whereAdded = true
query += " ${MSchedule.ARRIVAL} = $arrival"
}
departure?.let {
query += if (whereAdded) {
" AND"
} else {
" WHERE"
}
whereAdded = true
query += if (whereAdded) " AND" else " WHERE"; whereAdded = true
query += " ${MSchedule.DEPARTURE} = $departure"
}
deleteCount++
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/org/mtransit/parser/db/SQLUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,30 @@ object SQLUtils {
)
}

private val ESCAPE_ID: CharSequenceTranslator by lazy {
LookupTranslator(
mapOf(
QUOTE to "$QUOTE$QUOTE",
)
)
}

@JvmStatic
fun escape(string: String): String {
return ESCAPE.translate(string)
}

@JvmStatic
fun escapeId(string: String): String {
return ESCAPE_ID.translate(string)
}

@JvmName("escapeExt")
fun String.escape() = escape(this)

@JvmName("escapeIdExt")
fun String.escapeId() = escapeId(this)

@JvmStatic
fun quotes(string: String): String {
return SQLUtilsCommons.escapeString(string)
Expand All @@ -55,6 +71,8 @@ object SQLUtils {

fun String.quotesEscape() = escape(this).quotes()

fun String.quotesEscapeId() = escapeId(this).quotes()

@JvmStatic
fun unquotes(string: String): String {
return string.trim { it == QUOTE_ }
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/mtransit/parser/gtfs/data/GCalendar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.mtransit.parser.gtfs.data

import androidx.annotation.Discouraged
import org.mtransit.parser.MTLog
import org.mtransit.parser.db.SQLUtils.escape
import org.mtransit.parser.db.SQLUtils.escapeId
import java.util.Calendar

// https://developers.google.com/transit/gtfs/reference#calendar_fields
Expand Down Expand Up @@ -75,7 +75,7 @@ data class GCalendar(
get() = GIDs.getString(serviceIdInt)

val escapedServiceId: String
get() = _serviceId.escape()
get() = _serviceId.escapeId()

val escapedServiceIdInt: Int
get() = escapedServiceId.toGIDInt()
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/mtransit/parser/gtfs/data/GCalendarDate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package org.mtransit.parser.gtfs.data
import androidx.annotation.Discouraged
import org.mtransit.commons.gtfs.data.CalendarDate
import org.mtransit.parser.MTLog
import org.mtransit.parser.db.SQLUtils.escape
import org.mtransit.parser.gtfs.GAgencyTools
import org.mtransit.parser.db.SQLUtils.escapeId
import org.mtransit.parser.gtfs.data.GFieldTypes.isAfter
import org.mtransit.parser.gtfs.data.GFieldTypes.isBefore
import org.mtransit.parser.gtfs.data.GFieldTypes.isBetween
Expand Down Expand Up @@ -48,7 +47,7 @@ data class GCalendarDate(
get() = GIDs.getString(serviceIdInt)

val escapedServiceId: String
get() = _serviceId.escape()
get() = _serviceId.escapeId()

val escapedServiceIdInt: Int
get() = escapedServiceId.toGIDInt()
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/org/mtransit/parser/mt/GenerateMObjectsTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.mtransit.parser.mt.data.MServiceDate;
import org.mtransit.parser.mt.data.MSpec;
import org.mtransit.parser.mt.data.MStop;
import org.mtransit.parser.mt.data.MTrip;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -92,6 +93,7 @@ private MSpec doCall() {
HashMap<Long, MRoute> mRoutes = new HashMap<>();
HashMap<Long, MDirection> mDirections = new HashMap<>();
HashMap<String, MDirectionStop> allMDirectionStops = new HashMap<>();
HashMap<Integer, MTrip> mTrips = new HashMap<>();
HashMap<Integer, MStop> mStops = new HashMap<>();
HashSet<Integer> directionStopIds = new HashSet<>(); // the list of stop IDs used by directions
HashSet<Integer> serviceIdInts = new HashSet<>();
Expand Down Expand Up @@ -136,6 +138,7 @@ private MSpec doCall() {
mAgencies,
mRoutes,
mDirections,
mTrips,
mStops,
allMDirectionStops,
directionStopIds,
Expand Down Expand Up @@ -168,6 +171,8 @@ private MSpec doCall() {
Collections.sort(mAgenciesList);
ArrayList<MStop> mStopsList = new ArrayList<>(mStops.values());
Collections.sort(mStopsList);
ArrayList<MTrip> mTripsList = new ArrayList<>(mTrips.values());
Collections.sort(mTripsList);
ArrayList<MRoute> mRoutesList = new ArrayList<>(mRoutes.values());
Collections.sort(mRoutesList);
ArrayList<MDirection> mDirectionsList = new ArrayList<>(mDirections.values());
Expand All @@ -176,7 +181,7 @@ private MSpec doCall() {
Collections.sort(mDirectionStopsList);
setDirectionStopNoPickup(mDirectionStopsList, mSchedules.values());
ArrayList<MServiceDate> mServiceDatesList = new ArrayList<>(mServiceDates);
Collections.sort(mServiceDatesList);
mServiceDatesList.sort(MServiceDate.getCOMPARATOR_BY_CALENDAR_DATE());
ArrayList<MFrequency> mFrequenciesList = new ArrayList<>(mFrequencies.values());
Collections.sort(mFrequenciesList);
TreeMap<Long, List<MFrequency>> mRouteFrequencies = new TreeMap<>();
Expand Down Expand Up @@ -257,12 +262,13 @@ private MSpec doCall() {
throw new MTLog.Fatal(e, "Error while parsing dates '%s %s'!", lastCalendarDate, lastDeparture);
}
}
MSpec mRouteSpec = new MSpec(
final MSpec mRouteSpec = new MSpec(
mAgenciesList,
mStopsList,
mRoutesList,
mDirectionsList,
mDirectionStopsList,
mTripsList,
mServiceDatesList,
mRouteFrequencies,
firstTimestamp,
Expand All @@ -279,6 +285,7 @@ private void parseRDS(HashMap<String, MSchedule> mSchedules,
HashMap<Integer, MAgency> mAgencies,
HashMap<Long, MRoute> mRoutes,
HashMap<Long, MDirection> mDirections,
HashMap<Integer, MTrip> mTrips,
HashMap<Integer, MStop> mStops,
HashMap<String, MDirectionStop> allMDirectionStops,
HashSet<Integer> directionStopIds,
Expand Down Expand Up @@ -341,6 +348,7 @@ private void parseRDS(HashMap<String, MSchedule> mSchedules,
mSchedules,
mFrequencies,
mDirections,
mTrips,
mStops,
serviceIdInts,
mRoute,
Expand Down Expand Up @@ -450,6 +458,7 @@ private void fixRouteLongName(HashMap<Long, MRoute> mRoutes, HashMap<Long, MDire
private void parseGTrips(HashMap<String, MSchedule> mSchedules,
HashMap<String, MFrequency> mFrequencies,
HashMap<Long, MDirection> mDirections,
HashMap<Integer, MTrip> mTrips,
HashMap<Integer, MStop> mStops,
HashSet<Integer> serviceIdInts,
MRoute mRoute,
Expand Down Expand Up @@ -603,6 +612,7 @@ private void parseGTrips(HashMap<String, MSchedule> mSchedules,
continue;
}
mDirections.put(mDirection.getId(), mDirection);
mTrips.put(gTrip.getTripIdInt(), MTrip.from(routeId, mDirection.getId(), gTrip));
}
if (g++ % 10 == 0) { // LOG
MTLog.logPOINT(); // LOG
Expand Down
Loading
Loading