Skip to content

Commit

Permalink
Fix trip head-sign "''" instead of null/empty
Browse files Browse the repository at this point in the history
  • Loading branch information
mmathieum committed Feb 16, 2024
1 parent a732456 commit d55c2f9
Show file tree
Hide file tree
Showing 18 changed files with 194 additions and 115 deletions.
2 changes: 0 additions & 2 deletions src/main/java/org/mtransit/parser/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,4 @@ public final class Constants {

public static final String SPACE_ = " ";

public static final String UUID_SEPARATOR = "-";

}
1 change: 1 addition & 0 deletions src/main/java/org/mtransit/parser/MTLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public static void logFatal(@NotNull Throwable t, @NotNull String format, @Nulla
}

public static void waitForEnter() {
// if (true) return;
if (DefaultAgencyTools.IS_CI) {
return;
}
Expand Down
21 changes: 7 additions & 14 deletions src/main/java/org/mtransit/parser/db/DBUtils.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.mtransit.parser.db

import org.mtransit.commons.sql.SQLCreateBuilder
import org.mtransit.commons.sql.getStringOrNull
import org.mtransit.parser.DefaultAgencyTools
import org.mtransit.parser.FileUtils
import org.mtransit.parser.MTLog
import org.mtransit.parser.db.SQLUtils.quotesEscape
import org.mtransit.parser.gtfs.data.GStopTime
import org.mtransit.parser.gtfs.data.GTripStop
import org.mtransit.parser.mt.data.MSchedule
Expand All @@ -24,7 +26,6 @@ object DBUtils {
private const val SCHEDULES_TABLE_NAME = "m_schedule"

private const val SQL_RESULT_ALIAS = "result"
private const val SQL_NULL = "null"

private val IS_USING_FILE_INSTEAD_OF_MEMORY = DefaultAgencyTools.IS_CI

Expand Down Expand Up @@ -150,7 +151,7 @@ object DBUtils {
setInt(idx++, gStopTime.stopSequence)
setInt(idx++, gStopTime.arrivalTime)
setInt(idx++, gStopTime.departureTime)
setString(idx++, "${gStopTime.stopHeadsign?.let { SQLUtils.quotes(SQLUtils.escape(it)) }}")
setString(idx++, gStopTime.stopHeadsign?.quotesEscape())
setInt(idx++, gStopTime.pickupType.id)
setInt(idx++, gStopTime.dropOffType.id)
setInt(idx++, gStopTime.timePoint.id)
Expand Down Expand Up @@ -186,7 +187,7 @@ object DBUtils {
"${gStopTime.stopSequence}," +
"${gStopTime.arrivalTime}," +
"${gStopTime.departureTime}," +
"${gStopTime.stopHeadsign?.let { SQLUtils.quotes(SQLUtils.escape(it)) }}," +
"${gStopTime.stopHeadsign?.quotesEscape()}," +
"${gStopTime.pickupType.id}," +
"${gStopTime.dropOffType.id}," +
"${gStopTime.timePoint.id}" +
Expand Down Expand Up @@ -231,7 +232,7 @@ object DBUtils {
"${mSchedule.pathIdInt}," +
"${mSchedule.accessible}," +
"${mSchedule.headsignType}," +
"${mSchedule.headsignValue?.let { SQLUtils.quotes(SQLUtils.escape(it)) }}" +
"${mSchedule.headsignValue?.quotesEscape()}" +
SQLUtilsCommons.P2
)
insertRowCount++
Expand Down Expand Up @@ -276,18 +277,14 @@ object DBUtils {
connection.createStatement().use { statement ->
val rs = SQLUtils.executeQuery(statement, query)
while (rs.next()) {
var stopHeadSign: String? = rs.getString(GStopTime.STOP_HEADSIGN)
if (stopHeadSign == SQL_NULL) {
stopHeadSign = null
}
result.add(
GStopTime(
rs.getInt(GStopTime.TRIP_ID),
rs.getInt(GStopTime.ARRIVAL_TIME),
rs.getInt(GStopTime.DEPARTURE_TIME),
rs.getInt(GStopTime.STOP_ID),
rs.getInt(GStopTime.STOP_SEQUENCE),
stopHeadSign,
rs.getStringOrNull(GStopTime.STOP_HEADSIGN),
rs.getInt(GStopTime.PICKUP_TYPE),
rs.getInt(GStopTime.DROP_OFF_TYPE),
rs.getInt(GStopTime.TIME_POINT),
Expand Down Expand Up @@ -484,10 +481,6 @@ object DBUtils {
connection.createStatement().use { statement ->
val rs = SQLUtils.executeQuery(statement, query)
while (rs.next()) {
var headsignValue: String? = rs.getString(MSchedule.HEADSIGN_VALUE)
if (headsignValue == SQL_NULL) {
headsignValue = null
}
result.add(
MSchedule(
rs.getLong(MSchedule.ROUTE_ID),
Expand All @@ -499,7 +492,7 @@ object DBUtils {
rs.getInt(MSchedule.PATH_ID),
rs.getInt(MSchedule.WHEELCHAIR_BOARDING),
rs.getInt(MSchedule.HEADSIGN_TYPE),
headsignValue
rs.getStringOrNull(MSchedule.HEADSIGN_VALUE),
)
)
selectRowCount++
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/mtransit/parser/db/SQLUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ object SQLUtils {
return "'$string'"
}

@JvmName("quotesExt")
fun String.quotes() = quotes(this)

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

@JvmStatic
fun execute(statement: Statement, query: String): Boolean {
if (org.mtransit.parser.Constants.LOG_SQL) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/mtransit/parser/gtfs/GAgencyTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public interface GAgencyTools {
@NotNull
String cleanRouteLongName(@NotNull String routeLongName);

@SuppressWarnings("DeprecatedIsStillUsed")
@Deprecated
boolean allowGTFSIdOverride();

Expand Down
36 changes: 12 additions & 24 deletions src/main/java/org/mtransit/parser/gtfs/GReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.mtransit.parser.gtfs.data.GSpec;
import org.mtransit.parser.gtfs.data.GStop;
import org.mtransit.parser.gtfs.data.GStopTime;
import org.mtransit.parser.gtfs.data.GTimePoint;
import org.mtransit.parser.gtfs.data.GTrip;

import java.io.BufferedReader;
Expand Down Expand Up @@ -63,16 +62,16 @@ public static GSpec readGtfsZipFile(@NotNull String gtfsFile,
try {
// AGENCY
if (!calendarsOnly) {
readFile(agencyTools, gtfsDir, GAgency.FILENAME, true, line ->
readFile(gtfsDir, GAgency.FILENAME, true, line ->
processAgency(agencyTools, gSpec, line)
);
}
// CALENDAR DATES
boolean hasCalendarDates = readFile(agencyTools, gtfsDir, GCalendarDate.FILENAME, false, line ->
boolean hasCalendarDates = readFile(gtfsDir, GCalendarDate.FILENAME, false, line ->
processCalendarDate(agencyTools, gSpec, line)
);
// CALENDAR
boolean hasCalendars = readFile(agencyTools, gtfsDir, GCalendar.FILENAME, false, line ->
boolean hasCalendars = readFile(gtfsDir, GCalendar.FILENAME, false, line ->
processCalendar(agencyTools, gSpec, line)
);
boolean hasCalendar = hasCalendarDates || hasCalendars;
Expand All @@ -81,25 +80,25 @@ public static GSpec readGtfsZipFile(@NotNull String gtfsFile,
}
// ROUTES
if (!calendarsOnly) {
readFile(agencyTools, gtfsDir, GRoute.FILENAME, true, line ->
readFile(gtfsDir, GRoute.FILENAME, true, line ->
processRoute(agencyTools, gSpec, line)
);
}
// TRIPS
if (!calendarsOnly) {
readFile(agencyTools, gtfsDir, GTrip.FILENAME, true, line ->
readFile(gtfsDir, GTrip.FILENAME, true, line ->
processTrip(agencyTools, gSpec, line)
);
}
// FREQUENCIES
if (!calendarsOnly && !routeTripCalendarsOnly) {
readFile(agencyTools, gtfsDir, GFrequency.FILENAME, false, line ->
readFile(gtfsDir, GFrequency.FILENAME, false, line ->
processFrequency(agencyTools, gSpec, line)
);
}
// STOPS
if (!calendarsOnly && !routeTripCalendarsOnly) {
readFile(agencyTools, gtfsDir, GStop.FILENAME, true, line ->
readFile(gtfsDir, GStop.FILENAME, true, line ->
processStop(agencyTools, gSpec, line)
);
}
Expand All @@ -108,7 +107,7 @@ public static GSpec readGtfsZipFile(@NotNull String gtfsFile,
MTLog.log("- IDs: %d", GIDs.count());
DBUtils.setAutoCommit(false);
final PreparedStatement insertStopTimePrepared = DBUtils.prepareInsertStopTime(false);
readFile(agencyTools, gtfsDir, GStopTime.FILENAME, false,
readFile(gtfsDir, GStopTime.FILENAME, false,
line -> processStopTime(agencyTools, gSpec, line, insertStopTimePrepared),
columnNames -> {
if (!columnNames.contains(GStopTime.PICKUP_TYPE)) {
Expand Down Expand Up @@ -140,17 +139,15 @@ public static GSpec readGtfsZipFile(@NotNull String gtfsFile,
}

private static boolean readFile(
@NotNull final GAgencyTools agencyTools,
@NotNull String gtfsDir,
@NotNull String fileName,
boolean fileRequired,
@NotNull LineProcessor lineProcessor
) {
return readFile(agencyTools, gtfsDir, fileName, fileRequired, lineProcessor, null);
return readFile(gtfsDir, fileName, fileRequired, lineProcessor, null);
}

private static boolean readFile(
@NotNull final GAgencyTools agencyTools,
@NotNull String gtfsDir,
@NotNull String fileName,
boolean fileRequired,
Expand Down Expand Up @@ -180,6 +177,7 @@ private static boolean readFile(

private static final Pattern QUOTE_ = Pattern.compile("\"");

@SuppressWarnings("unused")
private static void readCsv(String filename, BufferedReader reader,
LineProcessor lineProcessor) throws IOException {
readCsv(filename, reader, lineProcessor, null);
Expand Down Expand Up @@ -264,17 +262,7 @@ private static void readCsv(String filename, BufferedReader reader,

private static void processStopTime(GAgencyTools agencyTools, GSpec gSpec, HashMap<String, String> line, PreparedStatement insertStopTimePrepared) {
try {
final GStopTime gStopTime = new GStopTime(
line.get(GStopTime.TRIP_ID),
line.get(GStopTime.ARRIVAL_TIME).trim(),
line.get(GStopTime.DEPARTURE_TIME).trim(),
line.get(GStopTime.STOP_ID).trim(),
Integer.parseInt(line.get(GStopTime.STOP_SEQUENCE).trim()),
line.get(GStopTime.STOP_HEADSIGN),
GPickupType.parse(line.get(GStopTime.PICKUP_TYPE)),
GDropOffType.parse(line.get(GStopTime.DROP_OFF_TYPE)),
GTimePoint.parse(line.get(GStopTime.TIME_POINT))
);
final GStopTime gStopTime = GStopTime.fromLine(line);
if (agencyTools.excludeTripNullable(gSpec.getTrip(gStopTime.getTripIdInt()))) {
return;
}
Expand Down Expand Up @@ -389,7 +377,7 @@ private static void processTrip(GAgencyTools agencyTools, GSpec gSpec, HashMap<S
final String directionId = line.get(GTrip.DIRECTION_ID);
final String tripHeadsign = line.get(GTrip.TRIP_HEADSIGN);
final String wheelchairAccessible = line.get(GTrip.WHEELCHAIR_ACCESSIBLE);
GTrip gTrip = new GTrip(
final GTrip gTrip = new GTrip(
line.get(GTrip.ROUTE_ID),
line.get(GTrip.SERVICE_ID),
line.get(GTrip.TRIP_ID),
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/mtransit/parser/gtfs/data/GCalendarDate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ data class GCalendarDate(
const val DATE = "date"
const val EXCEPTION_DATE = "exception_type"

private const val UID_SEPARATOR = "0" // int IDs can be negative

@JvmStatic
fun getNewUID(
date: Int,
serviceIdInt: Int,
) = "${date}0${serviceIdInt}".toLong()
) = "${date}$UID_SEPARATOR${serviceIdInt}".toLong()

@JvmStatic
fun isServiceEntirelyRemoved(
Expand Down
73 changes: 44 additions & 29 deletions src/main/java/org/mtransit/parser/gtfs/data/GStopTime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ data class GStopTime(
dropOffTypeInt: Int,
timePointInt: Int,
) : this(
tripIdInt,
arrivalTime,
departureTime,
stopIdInt,
stopSequence,
stopHeadsign,
GPickupType.parse(pickupTypeInt),
GDropOffType.parse(dropOffTypeInt),
GTimePoint.parse(timePointInt),
tripIdInt = tripIdInt,
_arrivalTime = arrivalTime,
_departureTime = departureTime,
stopIdInt = stopIdInt,
stopSequence = stopSequence,
stopHeadsign = stopHeadsign,
pickupType = GPickupType.parse(pickupTypeInt),
dropOffType = GDropOffType.parse(dropOffTypeInt),
timePoint = GTimePoint.parse(timePointInt),
)

constructor(
Expand All @@ -52,15 +52,15 @@ data class GStopTime(
dropOffType: GDropOffType,
timePoint: GTimePoint,
) : this(
GIDs.getInt(tripId),
arrivalTime,
departureTime,
stopIdInt,
stopSequence,
stopHeadsign,
pickupType,
dropOffType,
timePoint,
tripIdInt = GIDs.getInt(tripId),
_arrivalTime = arrivalTime,
_departureTime = departureTime,
stopIdInt = stopIdInt,
stopSequence = stopSequence,
stopHeadsign = stopHeadsign,
pickupType = pickupType,
dropOffType = dropOffType,
timePoint = timePoint,
)

constructor(
Expand All @@ -74,15 +74,15 @@ data class GStopTime(
dropOffType: GDropOffType,
timePoint: GTimePoint,
) : this(
GIDs.getInt(tripId),
GTime.fromString(arrivalTime),
GTime.fromString(departureTime),
GIDs.getInt(stopId),
stopSequence,
stopHeadsign,
pickupType,
dropOffType,
timePoint,
tripIdInt = GIDs.getInt(tripId),
_arrivalTime = GTime.fromString(arrivalTime),
_departureTime = GTime.fromString(departureTime),
stopIdInt = GIDs.getInt(stopId),
stopSequence = stopSequence,
stopHeadsign = stopHeadsign,
pickupType = pickupType,
dropOffType = dropOffType,
timePoint = timePoint,
)

@Deprecated(message = "Not memory efficient")
Expand Down Expand Up @@ -136,7 +136,7 @@ data class GStopTime(

val uID by lazy { getNewUID(tripIdInt, stopIdInt, stopSequence) }

fun hasStopHeadsign() = !this.stopHeadsign.isNullOrEmpty()
fun hasStopHeadsign() = !this.stopHeadsign.isNullOrBlank()

@Suppress("unused")
val stopHeadsignOrDefault: String = stopHeadsign ?: StringUtils.EMPTY
Expand Down Expand Up @@ -190,12 +190,27 @@ data class GStopTime(
const val DROP_OFF_TYPE = "drop_off_type"
const val TIME_POINT = "timepoint"

@JvmStatic
fun fromLine(line: Map<String, String>) = GStopTime(
line[TRIP_ID] ?: throw MTLog.Fatal("Invalid GStopTime from $line!"),
line[ARRIVAL_TIME]?.trim() ?: throw MTLog.Fatal("Invalid GStopTime from $line!"),
line[DEPARTURE_TIME]?.trim() ?: throw MTLog.Fatal("Invalid GStopTime from $line!"),
line[STOP_ID]?.trim() ?: throw MTLog.Fatal("Invalid GStopTime from $line!"),
line[STOP_SEQUENCE]?.trim()?.toInt() ?: throw MTLog.Fatal("Invalid GStopTime from $line!"),
line[STOP_HEADSIGN]?.takeIf { it.isNotBlank() },
GPickupType.parse(line[PICKUP_TYPE]),
GDropOffType.parse(line[DROP_OFF_TYPE]),
GTimePoint.parse(line[TIME_POINT]),
)

private const val UID_SEPARATOR = "0" // int IDs can be negative

@JvmStatic
fun getNewUID(
tripIdInt: Int,
stopIdInt: Int,
stopSequence: Int
) = "${tripIdInt}0${stopIdInt}0${stopSequence}".toLong()
) = "${tripIdInt}$UID_SEPARATOR${stopIdInt}$UID_SEPARATOR${stopSequence}".toLong()

fun Iterable<GStopTime>.minStopSequence(): Int {
return this.minOfOrNull { it.stopSequence } ?: 0
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/org/mtransit/parser/gtfs/data/GTrip.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ data class GTrip(
tripShortName: String?,
wheelchairBoarding: Int?,
) : this(
GIDs.getInt(routeId),
GIDs.getInt(serviceId),
GIDs.getInt(tripId),
GDirectionId.parse(directionId),
tripHeadsign,
tripShortName,
GWheelchairBoardingType.parse(wheelchairBoarding),
routeIdInt = GIDs.getInt(routeId),
serviceIdInt = GIDs.getInt(serviceId),
tripIdInt = GIDs.getInt(tripId),
directionIdE = GDirectionId.parse(directionId),
tripHeadsign = tripHeadsign,
tripShortName = tripShortName,
wheelchairAccessible = GWheelchairBoardingType.parse(wheelchairBoarding),
)

val directionId: Int?
Expand Down
Loading

0 comments on commit d55c2f9

Please sign in to comment.