Skip to content

Commit

Permalink
mGTFS > fix Stop SQL select joins
Browse files Browse the repository at this point in the history
  • Loading branch information
mmathieum committed Nov 25, 2024
1 parent 499b336 commit c8bee1f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/main/java/org/mtransit/commons/gtfs/sql/CommonSQL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,7 @@ abstract class CommonSQL<MainType>() : TableSQL {
}

abstract fun fromResultSet(rs: ResultSet): MainType

fun getAlias(sourceColumn: String, valueColumn: String) = "${sourceColumn}_$valueColumn"
fun getJoinAlias(sourceColumn: String) = "the_${sourceColumn}"
}
13 changes: 8 additions & 5 deletions src/main/java/org/mtransit/commons/gtfs/sql/StopSQL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,14 @@ object StopSQL : CommonSQL<Stop>(), TableSQL {
fun select(stopId: StopId? = null, statement: Statement): List<Stop> {
val sql = buildString {
append("SELECT ")
append("* ")
append("*, ")
append("${getJoinAlias(T_STOP_K_ID_INT)}.$T_STOP_IDS_K_ID AS ${getAlias(T_STOP_K_ID_INT, T_STOP_IDS_K_ID)}, ")
append("${getJoinAlias(T_STOP_K_PARENT_STATION_ID_INT)}.$T_STOP_IDS_K_ID AS ${getAlias(T_STOP_K_PARENT_STATION_ID_INT, T_STOP_IDS_K_ID)} ")
append("FROM $T_STOP ")
append("LEFT JOIN $T_STOP_IDS ON $T_STOP.$T_STOP_K_ID_INT = $T_STOP_IDS.$T_STOP_K_ID_INT ")
append("LEFT JOIN $T_STOP_IDS AS ${getJoinAlias(T_STOP_K_ID_INT)} ON $T_STOP.$T_STOP_K_ID_INT =${getJoinAlias(T_STOP_K_ID_INT)}.$T_STOP_K_ID_INT ")
append("LEFT JOIN $T_STOP_IDS AS ${getJoinAlias(T_STOP_K_PARENT_STATION_ID_INT)} ON $T_STOP.$T_STOP_K_PARENT_STATION_ID_INT = ${getJoinAlias(T_STOP_K_PARENT_STATION_ID_INT)}.$T_STOP_K_ID_INT ")
stopId?.let {
append("WHERE $T_STOP_IDS.$T_STOP_IDS_K_ID = '$it'")
append("WHERE ${getJoinAlias(T_STOP_K_ID_INT)}.$T_STOP_IDS_K_ID = '$it'")
}
}
return statement.executeQuery(sql).use { rs ->
Expand All @@ -91,14 +94,14 @@ object StopSQL : CommonSQL<Stop>(), TableSQL {

override fun fromResultSet(rs: ResultSet) = with(rs) {
Stop(
stopId = getString(T_STOP_IDS_K_ID),
stopId = getString(getAlias(T_STOP_K_ID_INT, T_STOP_IDS_K_ID)),
stopCode = getString(T_STOP_K_STOP_CODE),
stopName = getString(T_STOP_K_STOP_NAME),
stopLat = getDouble(T_STOP_K_STOP_LAT),
stopLon = getDouble(T_STOP_K_STOP_LON),
stopUrl = getString(T_STOP_K_STOP_URL),
locationType = getInt(T_STOP_K_LOCATION_TYPE),
parentStationId = getString(T_STOP_K_PARENT_STATION_ID_INT),
parentStationId = getString(getAlias(T_STOP_K_PARENT_STATION_ID_INT, T_STOP_IDS_K_ID)),
wheelchairBoarding = getInt(T_STOP_K_WHEELCHAIR_BOARDING),
)
}
Expand Down

0 comments on commit c8bee1f

Please sign in to comment.